主要是使用內容提供者ContentProvider
#1.在activity_main.xml佈局文件中添加寫sdcard權限,並添加讀短信的權限
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:onClick="backupsms"
android:text="備份短信" />
</RelativeLayout>
#2.MainActivity中內容如下
package com.wzw.backupsms;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import com.wzw.backupsms.entity.SmsInfo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
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);
}
public void backupsms(View v){
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){
List<SmsInfo> smsList=new ArrayList<SmsInfo>();
SmsInfo smsInfo;
while(cursor.moveToNext()){ //控制遊標指針,向下移一位
smsInfo=new SmsInfo();
smsInfo.setId(cursor.getInt(0)); //設置ID
smsInfo.setAddress(cursor.getString(1)); //設置短信號碼
smsInfo.setDate(cursor.getLong(2)); //設置短信時間
smsInfo.setType(cursor.getInt(3)); //設置短信類型
smsInfo.setBody(cursor.getString(4)); //設置短信內容
smsList.add(smsInfo);
}
cursor.close();
WriteToLocal(smsList);
}
}
/**
* 序列化到本地
*/
private void WriteToLocal(List<SmsInfo> smsList){
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 : smsList) {
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, "type");
serializer.text(String.valueOf(smsInfo.getType()));
serializer.endTag(null, "type");
//寫時間
serializer.startTag(null, "date");
serializer.text(String.valueOf(smsInfo.getDate()));
serializer.endTag(null, "date");
//寫內容
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, "恭喜你,備份成功!", 0).show();
} catch (Exception e) {
Toast.makeText(this, "我去,備份失敗!", 0).show();
e.printStackTrace();
}
}
}
#3.寫入的結果
<smss>
<sms id="3">
<address>10086</address>
<type>1</type>
<date>1406446124317</date>
<body>just for test!</body>
</sms>
<sms id="2">
<address>110</address>
<type>1</type>
<date>1406446024971</date>
<body>world</body>
</sms>
<sms id="1">
<address>5556</address>
<type>1</type>
<date>1406446006018</date>
<body>hello</body>
</sms>
</smss>
記錄安卓開發點點滴滴。。