android内容提供者contentProvider_短信备份
2016-12-26 21:10
495 查看
一、项目目录结构
二、activity_main.xml界面
三、activity_main.xml代码
二、activity_main.xml界面
三、activity_main.xml代码
<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="com.zgs.smsBackup.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:onClick="click" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="83dp" android:text="备份短信" /> </RelativeLayout>四、MainActivity.java代码
package com.zgs.smsBackup; import java.io.File; import java.io.FileOutputStream; import org.xmlpull.v1.XmlSerializer; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.util.Xml; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击按钮查询短信内容 然后把短信内容进行备份 public void click(View v) { try { //[1]获取XmlSerializer的实例 XmlSerializer serializer = Xml.newSerializer(); //[2]设置序列化器参数 File file = new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup.xml"); System.out.println(Environment.getExternalStorageDirectory().getPath()); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "utf-8"); //[3]写xml文档开头 serializer.startDocument("utf-8", true); //[4]写xml的根节点 serializer.startTag(null, "smss"); //[5]构造uri Uri uri = Uri.parse("content://sms/"); //[6]由于短信的数据库已经通过内容提供者暴露出来 所以我们直接通过内容解析者查询 Cursor cursor = getContentResolver().query(uri, new String[]{"address","date","body"}, null, null, null); while(cursor.moveToNext()){ String address = cursor.getString(0); String date = cursor.getString(1); String body = cursor.getString(2); //[7]写sms节点 serializer.startTag(null, "sms"); //[8]写address节点 serializer.startTag(null, "address"); serializer.text(address); serializer.endTag(null, "address"); //[9]写date节点 serializer.startTag(null, "date"); serializer.text(date); serializer.endTag(null, "date"); //[10]写body节点 serializer.startTag(null, "body"); serializer.text(body); serializer.endTag(null, "body"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); fos.close(); System.out.println("写XML成功!!!"); } catch (Exception e) { e.printStackTrace(); } } }五、AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zgs.smsBackup" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- android 利用内容提供者备份短信(XML序列化器)
- Android核心基础-5.Android 数据存储与访问-4.ContentProvider 内容提供者-示例(监听短信)
- android内容提供者contentProvider_短信插入
- Android开发14——监听内容提供者ContentProvider的数据变化
- Android-内容提供者、ListView点击事件、观察者、监听短信/联系人
- Android--- ContentProvider(内容提供者)
- [Android Pro] 内容提供者ContentProvider的基本使用
- Android -- ContentProvider 内容提供者,创建和调用
- Android开发14——监听内容提供者ContentProvider的数据变化
- Android开发14――监听内容提供者ContentProvider的数据变化
- android 数据存储和访问方式四:ContentProvider(内容提供者、数据共享)详解
- Android四大组件之一之内容提供者ContentProvider
- Android开发之ContentProvider(内容提供者)
- Android开发13——内容提供者ContentProvider的基本使用
- Android ContentProvider(内容提供者)
- Android四大组件之一ContentProvider(内容提供者)
- Android项目实战--手机卫士18--读取用户的短信内容以及短信备份
- Android项目实战--手机卫士18--读取用户的短信内容以及短信备份
- Android 使用ContentProvider(内容提供者)共享数据