Android開發簡訊備份小例子

來源:互聯網
上載者:User

標籤: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>




記錄安卓開發點點滴滴。。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.