Android短信备份及数据插入实现代码解析

编辑: admin 分类: Android 发布时间: 2021-11-29 来源:互联网

实现备份短信到xml文件和像短信中插入一条数据

一、实现短信将备份到xml文件中

在布局文件中定义一个按钮,定义点击事件为copyClick

MainActivity.java:

package com.lgqrlchinese.heima76android_copysms;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
  //动态获取权限
  static final String[] PERMISSION = new String[]{
      Manifest.permission.READ_SMS,
      Manifest.permission.WRITE_EXTERNAL_STORAGE,
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //动态获取权限
    MyPermission();
  }

  //点击按钮查询短信数据库内容,然后备份
  public void copyClick(View view) {

    try {
      //获取xml序列化实例
      XmlSerializer serializer = Xml.newSerializer();
      //设置序列化参数
      File file = new File(Environment.getExternalStorageDirectory().getPath(), "smsBackUp.xml");
      FileOutputStream fos = new FileOutputStream(file);
      serializer.setOutput(fos, "utf-8");
      //开始写xml文档的开头
      serializer.startDocument("utf-8", true);
      //开始写根节点
      serializer.startTag(null, "smss");

      //短信数据库内容也通过内容提供者暴露出来,所以我们只需要通过内容解析者去操作数据库
      Uri uri = Uri.parse("content://sms/");
      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);

        //写sms节点
        serializer.startTag(null, "sms");

        //写address节点
        serializer.startTag(null, "address");
        serializer.text(address);
        serializer.endTag(null, "address");

        //写body节点
        serializer.startTag(null, "body");
        serializer.text(body);
        serializer.endTag(null, "body");

        //写date节点
        serializer.startTag(null, "date");
        serializer.text(date);
        serializer.endTag(null, "date");

        serializer.endTag(null, "sms");

      }

      serializer.endTag(null, "smss");
      serializer.endDocument();


    } catch (FileNotFoundException e) {
      e.printStackTrace();
      System.out.println("异常1");
    } catch (IOException e) {
      e.printStackTrace();
      System.out.println("异常2");
    }


  }

  //动态获取权限
  public void MyPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
      //Android 6.0申请权限
      ActivityCompat.requestPermissions(this, PERMISSION, 1);
    } else {
      Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
    }
  }

}

当然权限是必要的

 <uses-permission android:name="android.permission.READ_SMS"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这样就在SD卡根目录备份了xml文件

在真机6.0测试中遇到存储address崩溃问题,调试好久。先打印addredss,没有问题;存储死数据没有问题,最后发现在输出打印室有个address为null。查看手机,是因为有个短信草稿,我想了两个都能想到的的解决办法:

1.删除或发送手机里的草稿

2.在代码中进行判断

if (address != null) {

          serializer.startTag(null, "address");
          serializer.text(address);
          serializer.endTag(null, "address");
        } else {
          serializer.text("这是草稿");
        }

二、向短信的数据库中插入数据(5.0及之后就不允许外部应用修改,直允许读取,不做过多记录)

import android.Manifest;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  //动态获取权限
  static final String[] PERMISSION = new String[]{
      Manifest.permission.READ_SMS,
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyPermission();
  }

  //点击按钮在短信数据库插入数据
  public void insertClick(View view) {
    Uri uri = Uri.parse("ocntent://sms");
    ContentValues values = new ContentValues();
    values.put("address", "17865318803");
    values.put("body", "我是你的影子");
    values.put("date", System.currentTimeMillis());
    getContentResolver().insert(uri, values);
  }

  //动态获取权限
  public void MyPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
      //Android 6.0申请权限
      ActivityCompat.requestPermissions(this, PERMISSION, 1);
    } else {
      Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。

【原URL http://www.yidunidc.com/jap.html 请说明出处】