Implement backup of SMS to XML files and insert a piece of data like SMS
1. Implement SMS backup to xml file
Define a button in the layout file and define the click event as copyClick
:
package .heima76android_copysms; import ; import ; import ; import ; import ; import .; import .; import .; import ; import ; import ; import ; import .; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { //Dynamic access static final String[] PERMISSION = new String[]{ .READ_SMS, .WRITE_EXTERNAL_STORAGE, }; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); //Dynamic access MyPermission(); } //Click the button to query the SMS database content, and then back up public void copyClick(View view) { try { //Get xml serialization instance XmlSerializer serializer = (); //Set serialization parameters File file = new File(().getPath(), ""); FileOutputStream fos = new FileOutputStream(file); (fos, "utf-8"); //Start the beginning of the xml document ("utf-8", true); //Start write the root node (null, "smss"); //The content of the SMS database is also exposed through the content provider, so we only need to operate the database through the content parser Uri uri = ("content://sms/"); Cursor cursor = getContentResolver().query(uri, new String[]{"address", "date", "body"}, null, null, null); while (()) { String address = (0); String date = (1); String body = (2); //Write sms node (null, "sms"); //Write address node (null, "address"); (address); (null, "address"); //Write body node (null, "body"); (body); (null, "body"); //Write date node (null, "date"); (date); (null, "date"); (null, "sms"); } (null, "smss"); (); } catch (FileNotFoundException e) { (); ("Exception 1"); } catch (IOException e) { (); ("Exception 2"); } } //Dynamic access public void MyPermission() { if ((this, .READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { //Android 6.0 application permission (this, PERMISSION, 1); } else { (this, "success", Toast.LENGTH_SHORT).show(); } } }
Of course permissions are necessary
<uses-permission android:name=".READ_SMS"/>
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
This way, the xml file is backed up in the root directory of the SD card
I encountered a storage address crash problem in real machine 6.0 test, and I debugged it for a long time. Print addredss first, there is no problem; there is no problem storing dead data, and finally I found that there is an address null in the output printing room. I checked my phone because there was a draft text message. I thought of two solutions that I could think of:
1. Delete or send drafts from your phone
2. Make judgments in the code
if (address != null) { (null, "address"); (address); (null, "address"); } else { ("This is a draft"); }
2. Insert data into the SMS database (5.0 and after, external applications will not be modified, and read will be allowed directly without too many records)
import ; import ; import ; import ; import .; import .; import .; import ; import ; import ; public class MainActivity extends AppCompatActivity { //Dynamic access static final String[] PERMISSION = new String[]{ .READ_SMS, }; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); MyPermission(); } //Click the button to insert data in the SMS database public void insertClick(View view) { Uri uri = ("ocntent://sms"); ContentValues values = new ContentValues(); ("address", "17865318803"); ("body", "I am your shadow"); ("date", ()); getContentResolver().insert(uri, values); } //Dynamic access public void MyPermission() { if ((this, .READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { //Android 6.0 application permission (this, PERMISSION, 1); } else { (this, "success", Toast.LENGTH_SHORT).show(); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.