对于上一节的相亲神器,大家应该知道怎么用内容提供者去得到系统的数据 进行开发了。
其实对于大部分的URI 可以百度去得到,但是我建议自己倒弄出源码 自己去研究下。
短信URI content://sms/
短信的数据库第一个是id ,第二个是address (地址)第三个日期 (date)第四个type(类型)第五个是body(内容)
demo的思路主要是 拿到接口以后 直接查询 然后序列化到本地 储存起来 保存在SD卡 ,记住加权限还有读取短信的权限
先看实体类吧 因为查询到结果需要储存起来
package com.example.backSms.entrys;
public class Smsinfo {
private int id;
private int type;
private String address;
private String body;
private long date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public Smsinfo(int id, int type, String address, String body, long date) {
super();
this.id = id;
this.type = type;
this.address = address;
this.body = body;
this.date = date;
}
@Override
public String toString() {
return "Smsinfo [id=" + id + ", type=" + type + ", address=" + address
+ ", body=" + body + ", date=" + date + "]";
}
public Smsinfo() {
super();
}
}
然后是主体类
<pre name="code" class="java">package com.example.backSms;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import com.example.backSms.entrys.Smsinfo;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**备份短信
* @param view
*/
public void btn(View view){
//查处所有短信
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id","address","date","type","body"}, null, null, null);
if (cursor!=null&&cursor.getCount()>0) {
Smsinfo smsinfo=null;
List<Smsinfo>list=new ArrayList<Smsinfo>();
while (cursor.moveToNext()) {
smsinfo=new Smsinfo();
smsinfo.setId(cursor.getInt(0));
smsinfo.setAddress(cursor.getString(1));
smsinfo.setDate(cursor.getLong(2));
smsinfo.setType(cursor.getInt(3));
smsinfo.setBody(cursor.getString(4));
list.add(smsinfo);
}
cursor.close();
//2.序列化到本地
writeTolocal(list);
}
}
private void writeTolocal(List<Smsinfo>smsinfos){
XmlSerializer serializer=Xml.newSerializer();//得到序列化对象
//指定输出位置
try {
FileOutputStream fos=new FileOutputStream("/mnt/sdcard/sms.xml");
serializer.setOutput(fos,"utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss");
for (Smsinfo smsinfo : smsinfos) {
serializer.startTag(null, "sms");
serializer.attribute(null, "id", String.valueOf(smsinfo.getId()));
serializer.startTag(null, "address");
serializer.text(smsinfo.getAddress());
serializer.endTag(null, "address");
serializer.startTag(null, "date");
serializer.text(String.valueOf(smsinfo.getDate()));
serializer.endTag(null, "date");
serializer.startTag(null, "type");
serializer.text(String.valueOf(smsinfo.getType()));
serializer.endTag(null, "type");
serializer.startTag(null, "body");
serializer.text(smsinfo.getBody());
serializer.endTag(null, "body");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smss");
serializer.endDocument();
Toast.makeText(this, "序列化备份成功", 3000).show();
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(this, "备份失败", 3000).show();
e.printStackTrace();
}
}
}