基于安卓短信的备份和恢复

我做的是将短信备份到本地的一个xml文件中,简单方便。(注:适用于android5.0以前,毕竟5.0后增加了数据的安全性,没有了写入短信的权限。)

//创建XMl文件
    public boolean xmlCreate() throws IOException {
        String path = ESD_PATH + "/SMSBack";
        File file = new File(path);

        if (!file.exists()) {
            file.mkdirs();
        }
        File file1 = new File(path, "message.xml");
        try {
            //建立输出流
            fos = new FileOutputStream(file1);
            serializer = Xml.newSerializer();
            serializer.setOutput(fos, "UTF-8");
            serializer.startDocument("UTF-8", true);
            serializer.startTag("SMS", "sms");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Cursor cursor = null;

        try {
            // type=1是收件箱,==2是发件箱;read=0表示未读,read=1表示读过,seen=0表示未读,seen=1表示读过
            String[] projection = new String[]{Telephony.Sms.ADDRESS, Telephony.Sms.PERSON, Telephony.Sms.DATE, Telephony.Sms.PROTOCOL, Telephony.Sms.READ,
                    Telephony.Sms.STATUS, Telephony.Sms.TYPE, Telephony.Sms.REPLY_PATH_PRESENT, Telephony.Sms.BODY, Telephony.Sms.LOCKED, Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN};
            Uri uri = Uri.parse(SMS_URI_ALL);
            cursor = resolver.query(uri, projection, null, null, "_id asc");
            if (cursor.moveToFirst()) {
//                String subject;
//                String service_center;

                String address;
                String person;
                String date;
                String protocol;
                String read;
                String status;
                String type;
                String reply_path_present;
                String body;
                String locked;
                String error_code;
                String seen;

                //生成子标签
                do {

                    // 如果address == null,xml文件中是不会生成该属性的,为了保证解析时,属性能够根据索引一一对应,必须要保证所有的item标记的属性数量和顺序是一致的
                    address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
                    if (address == null) {
                        address = "";
                    }
                    person = cursor.getString(cursor.getColumnIndex(Telephony.Sms.PERSON));
                    if (person == null) {
                        person = "";
                    }
                    date = cursor.getString(cursor.getColumnIndex(Telephony.Sms.DATE));
                    if (date == null) {
                        date = "";
                    }
                    protocol = cursor.getString(cursor.getColumnIndex(Telephony.Sms.PROTOCOL));
                    if (protocol == null) {// 为了便于xml解析
                        protocol = "";
                    }
                    read = cursor.getString(cursor.getColumnIndex(Telephony.Sms.READ));
                    if (read == null) {
                        read = "";
                    }
                    status = cursor.getString(cursor.getColumnIndex(Telephony.Sms.STATUS));
                    if (status == null) {
                        status = "";
                    }
                    type = cursor.getString(cursor.getColumnIndex(Telephony.Sms.TYPE));
                    if (type == null) {
                        type = "";
                    }
                    reply_path_present = cursor.getString(cursor.getColumnIndex(Telephony.Sms.REPLY_PATH_PRESENT));
                    if (reply_path_present == null) {// 为了便于XML解析
                        reply_path_present = "";
                    }
                    body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
                    if (body == null) {
                        body = "";
                    }
                    locked = cursor.getString(cursor.getColumnIndex(Telephony.Sms.LOCKED));
                    if (locked == null) {
                        locked = "";
                    }
                    error_code = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ERROR_CODE));
                    if (error_code == null) {
                        error_code = "";
                    }
                    seen = cursor.getString(cursor.getColumnIndex(Telephony.Sms.SEEN));
                    if (seen == null) {
                        seen = "";
                    }
                    // 生成xml子标记
                    // 开始标记
                    serializer.startTag("SMS", "item");
                    // 加入属性
                    serializer.attribute("SMS", Telephony.Sms.ADDRESS,address);
                    serializer.attribute("SMS", Telephony.Sms.PERSON, person);
                    serializer.attribute("SMS", Telephony.Sms.DATE, date);
                    serializer.attribute("SMS", Telephony.Sms.PROTOCOL, protocol);
                    serializer.attribute("SMS", Telephony.Sms.READ, read);
                    serializer.attribute("SMS", Telephony.Sms.STATUS, status);
                    serializer.attribute("SMS", Telephony.Sms.TYPE, type);
                    serializer.attribute("SMS", Telephony.Sms.REPLY_PATH_PRESENT, reply_path_present);
                    try {
                        serializer.attribute("SMS", Telephony.Sms.BODY, body);
                    } catch (Exception e) {
                        Toast.makeText(context, body + "~不符合规格", Toast.LENGTH_SHORT).show();
                    }
                    serializer.attribute("SMS", Telephony.Sms.LOCKED, locked);
                    serializer.attribute("SMS", Telephony.Sms.ERROR_CODE, error_code);
                    serializer.attribute("SMS", Telephony.Sms.SEEN, seen);
                    // 结束标记
                    serializer.endTag("SMS", "item");

                } while (cursor.moveToNext());
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("ERROR", e.toString());
        } finally {
            if (cursor != null) {
                //回收资源
                cursor.close();
            }
        }

        serializer.endTag("SMS", "sms");
        serializer.endDocument();
        fos.flush();
        fos.close();

        return true;
    }

可查看xml是否数据完整,不完整是由于某个短信的格式不对的问题。

当然可以换一种格式,如以下方法,自行尝试。

serializer.startTag(namespace, name);
                    serializer.text(text);
                    serializer.endTag(namespace, name);
//恢复短信
    public void recoverSMS() {
        smsItems = this.getXmlItems();

        for (SmsItem smsItem : smsItems) {

            Cursor cursor = resolver.query(Uri.parse(SMS_URI_ALL), new String[]{Telephony.Sms.DATE},
                    Telephony.Sms.DATE + "=?", new String[]{smsItem.getDate()}, null);

            if (!cursor.moveToFirst()) {
                ContentValues values = new ContentValues();
                values.put(Telephony.Sms.ADDRESS, smsItem.getAddress());
                values.put(Telephony.Sms.PERSON, smsItem.getPerson().equals("") ? null : smsItem.getPerson());
                values.put(Telephony.Sms.DATE, smsItem.getDate());
                values.put(Telephony.Sms.PROTOCOL, smsItem.getProtocol().equals("") ? null : smsItem.getProtocol());
                values.put(Telephony.Sms.READ, smsItem.getRead());
                values.put(Telephony.Sms.STATUS, smsItem.getStatus());
                values.put(Telephony.Sms.TYPE, smsItem.getType());
                values.put(Telephony.Sms.REPLY_PATH_PRESENT, smsItem.getReply_path_present());
                values.put(Telephony.Sms.BODY, smsItem.getBody());
                values.put(Telephony.Sms.LOCKED, smsItem.getLocked());
                values.put(Telephony.Sms.ERROR_CODE, smsItem.getError_code());
                values.put(Telephony.Sms.SEEN, smsItem.getSeen());
                resolver.insert(Uri.parse(SMS_URI_ALL), values);
            }
            cursor.close();
        }
        Log.d(TAG, "getXmlItems complete");
    }

    //从XMl文件中获取到短信数据
    public List<SmsItem> getXmlItems() {
        SmsItem smsItem = null;
        XmlPullParser pullParser = Xml.newPullParser();
        String path = ESD_PATH + "/SMSBack/message.xml";
        File file = new File(path);

        //当文件不存在时
        if (!file.exists()) {
            Toast.makeText(context, "当前无备份文件", Toast.LENGTH_SHORT).show();
            return null;
        }

        try {

            FileInputStream fis = new FileInputStream(file);
            pullParser.setInput(fis, "UTF-8");
            int event = pullParser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        smsItems = new ArrayList<>();
                        break;
                    case XmlPullParser.START_TAG:
                        if ("item".equals(pullParser.getName())) {
                            smsItem = new SmsItem();

                            smsItem.setAddress(pullParser.getAttributeValue(0));
                            smsItem.setPerson(pullParser.getAttributeValue(1));
                            smsItem.setDate(pullParser.getAttributeValue(2));
                            smsItem.setProtocol(pullParser.getAttributeValue(3));
                            smsItem.setRead(pullParser.getAttributeValue(4));
                            smsItem.setStatus(pullParser.getAttributeValue(5));
                            smsItem.setType(pullParser.getAttributeValue(6));
                            smsItem.setReply_path_present(pullParser.getAttributeValue(7));
                            smsItem.setBody(pullParser.getAttributeValue(8));
                            smsItem.setLocked(pullParser.getAttributeValue(9));
                            smsItem.setError_code(pullParser.getAttributeValue(10));
                            smsItem.setSeen(pullParser.getAttributeValue(11));
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("item".equals(pullParser.getName())) {
                            smsItems.add(smsItem);
                            Log.d(TAG, smsItem.toString());
                            smsItem = null;
                        }
                        break;
                }
                event = pullParser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return smsItems;
    }

相关推荐