Self-made SMS generator source code in Android

Source: Internet
Author: User

Text message Entity bean

package com.examp.backsms.domain;public class SmsInfo {private String address;private String date;private String type;private String body;public SmsInfo() {}public SmsInfo(String address, String date, String type, String body) {this.address = address;this.date = date;this.type = type;this.body = body;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}@Overridepublic String toString() {return "SmsInfo [address=" + address + ", date=" + date + ", type="+ type + ", body=" + body + "]";}}

Core code
Package com. examp. backsms; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. contentResolver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. view. view; import com. examp. backsms. domain. smsInfo; import com. examp. backsms. util. smsUtils; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void backSms (View view) {// create the called host name Uri uri = Uri. parse ("content: // sms/"); // obtain the content parser ContentResolver resolver = getContentResolver (); // execute the query and obtain the result set Cursor cursor = resolver. query (uri, new String [] {"address", "date", "type", "body"}, null); // create a List of text messages
 
  
Infos = new ArrayList
  
   
(); // Add them to the set one by one while (cursor. moveToNext () {SmsInfo info = new SmsInfo (); info. setAddress (cursor. getString (cursor. getColumnIndex ("address"); info. setBody (cursor. getString (cursor. getColumnIndex ("body"); info. setDate (cursor. getString (cursor. getColumnIndex ("date"); info. setType (cursor. getString (cursor. getColumnIndex ("type"); infos. add (info);} // call the backup service through the tool class // transfer the set | context of this activity to SmsUtils. backupSms (infos, this); cursor. close ();}}
  
 

Tool

Package com. examp. backsms. util; import java. io. file; import java. io. fileOutputStream; import java. util. list; import org. xmlpull. v1.XmlSerializer; import android. content. context; import android. OS. environment; import android. util. xml; import android. widget. toast; import com. examp. backsms. domain. smsInfo; public class SmsUtils {public static void backupSms (List
 
  
SmsInfos, Context context) {try {// create XML serializer = Xml. newSerializer (); File file = new File (Environment. getExternalStorageDirectory (), "backupSms. xml "); FileOutputStream OS = new FileOutputStream (file); // initialize the xml serializer, specify the file to which xml data is written, and specify the file encoding method serializer. setOutput (OS, "UTF-8"); // set
  , And whether it is an independent xml file serializer. startDocument ("UTF-8", true); // equivalent
  
   
Serializer. startTag (null, "smss"); for (SmsInfo info: smsInfos) {serializer. startTag (null, "sms"); serializer. startTag (null, "body"); serializer. text (info. getBody (); serializer. endTag (null, "body"); serializer. startTag (null, "type"); serializer. text (info. getType () + ""); serializer. endTag (null, "type"); serializer. startTag (null, "address"); serializer. text (info. getAddress (); serializer. endTag (null, "adsid Ss "); serializer. startTag (null, "date"); serializer. text (info. getDate () + ""); serializer. endTag (null, "date"); serializer. endTag (null, "sms");} serializer. endTag (null, "smss"); serializer. endDocument (); OS. close (); Toast. makeText (context, "the text message is successfully backed up, and the backup storage path is:" + Environment. getExternalStorageDirectory () + "backSms. xml ", Toast. LENGTH_SHORT ). show ();} catch (Exception e) {Toast. makeText (context, "failed to back up the short message !! ", Toast. LENGTH_SHORT). show (); e. printStackTrace ();}}}
  
 

View code, because it is only a case, there is only one button
     
  
 

Permission list file
 
     
      
      
      
      
      
                          
                                  
               
              
 

Resource file string. xml
 
     
  
   
0019 text message backup
      
  
   
Settings
      
  
   
Backup SMS
  
 

Note: This code is only a case code and cannot withstand excessive data pressure. Therefore, during actual development, data is read and backed up.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.