標籤:android 資料庫 安卓 sqlite 備份
主要是使用內容提供者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 {@Overrideprotected 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));//設定IDsmsInfo.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>
記錄安卓開發點點滴滴。。