标签:
1、获取短信的权限配置
1 <uses-permission android:name="android.permission.READ_SMS"/> 2 <uses-permission android:name="android.permission.WRITE_SMS"/>
2、通过内容提供者获取短信内容
1 package com.example.readsysmsm; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.example.readsysmsm.domain.SmsInfo; 7 8 import android.net.Uri; 9 import android.os.Bundle; 10 import android.app.Activity; 11 import android.content.ContentResolver; 12 import android.database.Cursor; 13 import android.view.View; 14 15 public class MainActivity extends Activity { 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 23 } 24 25 public void readAllMsm(View view){ 26 List<SmsInfo> smsInfos = new ArrayList<SmsInfo>(); 27 ContentResolver resolver = getContentResolver(); 28 Uri uri = Uri.parse("content://sms/"); 29 Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null); 30 while(cursor.moveToNext()){ 31 String address = cursor.getString(0); 32 Long date = cursor.getLong(1); 33 int type = cursor.getInt(2); 34 String body = cursor.getString(3); 35 SmsInfo smsInfo = new SmsInfo(date,type,body,address); 36 smsInfos.add(smsInfo); 37 } 38 39 SmsUtils.backup(smsInfos, this); 40 } 41 42 }
3、通过xml备份sms数据
1 package com.example.readsysmsm; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.util.List; 6 7 import org.xmlpull.v1.XmlSerializer; 8 9 import com.example.readsysmsm.domain.SmsInfo; 10 11 import android.content.Context; 12 import android.os.Environment; 13 import android.util.Xml; 14 import android.widget.Toast; 15 16 public class SmsUtils { 17 public static void backup(List<SmsInfo> smsInfos, Context context){ 18 try 19 { 20 XmlSerializer serializer = Xml.newSerializer(); 21 File file = new File(Environment.getExternalStorageDirectory(),"myback2.xml"); 22 FileOutputStream os = new FileOutputStream(file); 23 serializer.setOutput(os,"utf-8"); 24 serializer.startDocument("utf-8", true); 25 serializer.startTag(null, "smss"); 26 27 for(SmsInfo info : smsInfos) 28 { 29 serializer.startTag(null, "sms"); 30 31 serializer.startTag(null, "body"); 32 serializer.text(info.getBody()); 33 serializer.endTag(null, "body"); 34 35 serializer.startTag(null, "address"); 36 serializer.text(info.getAddress()); 37 serializer.endTag(null, "address"); 38 39 serializer.startTag(null, "type"); 40 serializer.text(info.getType() + ""); 41 serializer.endTag(null, "type"); 42 43 serializer.startTag(null, "date"); 44 serializer.text(info.getDate() + ""); 45 serializer.endTag(null, "date"); 46 47 serializer.endTag(null, "sms"); 48 } 49 50 serializer.endTag(null, "smss"); 51 serializer.endDocument(); 52 os.close(); 53 Toast.makeText(context, "生成成功!", 0).show(); 54 }catch(Exception e){ 55 e.printStackTrace(); 56 Toast.makeText(context, "生成失败!", 0).show(); 57 } 58 } 59 60 }
4、写数据进外存的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
5、信息类
1 package com.example.readsysmsm.domain; 2 3 public class SmsInfo { 4 private long date; 5 private int type; 6 private String body; 7 private String address; 8 9 public SmsInfo() 10 { 11 date = 0; 12 type = 1; 13 body = ""; 14 address = ""; 15 } 16 17 public SmsInfo(long date, int type, String body, String address) 18 { 19 this.date = date; 20 this.type = type; 21 this.body = body; 22 this.address = address; 23 } 24 25 public long getDate() 26 { 27 return date; 28 } 29 30 public void setDate(long date) 31 { 32 this.date = date; 33 } 34 35 public long getType() 36 { 37 return type; 38 } 39 40 public void setType(int type) 41 { 42 this.type = type; 43 } 44 45 public String getBody() 46 { 47 return body; 48 } 49 50 public void setBody(String body) 51 { 52 this.body = body; 53 } 54 55 public String getAddress() 56 { 57 return address; 58 } 59 60 public void setAddress(String address) 61 { 62 this.address = address; 63 } 64 }
标签:
原文地址:http://www.cnblogs.com/zhongyinghe/p/5301194.html