1.效果图:当点击按钮5s后插入短信
当点击备份:
备份的文件:
2.具体代码:
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
private Button btn_smsbackup;
private Button btn_smsinsert;
private List<SMSinfo> smSinfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
init();
//存放短信的集合
smSinfos =new ArrayList<SMSinfo>();
ContentResolver resolver=getContentResolver();
//指向内容提供者的URI
Uri uri=Uri.parse("content://sms/");
//读取短信内容
Cursor cursor=resolver.query(uri, new String[]{"type","date","address","body"}, null, null, null);
//遍历cursor,将内容读取到集合中
while (cursor.moveToNext()) {
int type=cursor.getInt(cursor.getColumnIndex("type"));
long date=cursor.getLong(cursor.getColumnIndex("date"));
String address=cursor.getString(cursor.getColumnIndex("address"));
String body=cursor.getString(cursor.getColumnIndex("body"));
SMSinfo smSinfo=new SMSinfo(address, type, date, body);
smSinfos.add(smSinfo);
}
//注册观察者
ContentResolver resolver2=getContentResolver();
resolver2.registerContentObserver(uri, true, new myobserver(new Handler()));
}
private void init() {
// TODO Auto-generated method stub
btn_smsbackup=(Button) findViewById(R.id.btn_smsbackup);
btn_smsinsert=(Button) findViewById(R.id.btn_smsinsert);
btn_smsbackup.setOnClickListener(this);
btn_smsinsert.setOnClickListener(this);
}
//创建一个继承ContentObserver的类,重写onChange方法,提醒改变
public class myobserver extends ContentObserver {
public myobserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
Toast.makeText(MainActivity.this, "观察到短信数据库内容变化了", Toast.LENGTH_SHORT).show();
super.onChange(selfChange);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//提取模拟器中的短信,通过序列化,存储到file文件
case R.id.btn_smsbackup:
BackUpSMS.buckupsms(smSinfos, MainActivity.this);
break;
//添加短信内容
case R.id.btn_smsinsert:
new Thread(){
public void run() {
try {
Thread.sleep(5000);
ContentResolver resolver=getContentResolver();
Uri uri=Uri.parse("content://sms/");
ContentValues values=new ContentValues();
values.put("type", 1);
values.put("date", System.currentTimeMillis());
values.put("address", 95555);
values.put("body", "短信插入测试");
resolver.insert(uri, values);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
break;
default:
break;
}
}
}
3.BackUpSMS.java
public class BackUpSMS {
public static void buckupsms(List<SMSinfo> smSinfos,Context context) {
try {
//将备份存储到sdcard中,使用XML的序列化
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"backup.xml");
FileOutputStream fos=new FileOutputStream(file);
XmlSerializer serializer=Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss");
for (SMSinfo smSinfo : smSinfos) {
serializer.startTag(null, "sms");
serializer.startTag(null, "type");
serializer.text(smSinfo.getType()+"");
serializer.endTag(null, "type");
serializer.startTag(null, "date");
serializer.text(smSinfo.getDate()+"");
serializer.endTag(null, "date");
serializer.startTag(null, "address");
serializer.text(smSinfo.getAddress());
serializer.endTag(null, "address");
serializer.startTag(null, "body");
serializer.text(smSinfo.getBody());
serializer.endTag(null, "body");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smss");
serializer.endDocument();
fos.close();
Toast.makeText(context, "备份成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.布局文件:
<Button
android:id="@+id/btn_smsbackup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="备份短信" />
<Button
android:id="@+id/btn_smsinsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_smsbackup"
android:layout_below="@+id/btn_smsbackup"
android:layout_marginTop="25dp"
android:text="5秒之后插入短信" />
5.最后一个问题是权限问题: