首页 > 代码库 > Android开发短信备份小例子

Android开发短信备份小例子

主要是使用内容提供者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>




记录安卓开发点点滴滴。。