/** * 备份短信的工具方法 * @param context 上下文 * @param fos 备份到哪个文件输出流里面. * @throws Exception */ public static void backUp(Context context,FileOutputStream fos) throws Exception {//在自己的方法中使用接口 ContentResolver resolver = context.getContentResolver(); Uri uri = Uri.parse("content://sms/"); XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "smss"); Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null); //pd.setMax(cursor.getCount());//设置初始化的总长度 int count = cursor.getCount(); //短信的总条数 int total = 0; while(cursor.moveToNext()){ serializer.startTag(null, "sms"); serializer.startTag(null, "address"); String address = cursor.getString(0); serializer.text(address); serializer.endTag(null, "address"); serializer.startTag(null, "date"); String date = cursor.getString(1); serializer.text(date); serializer.endTag(null, "date"); serializer.startTag(null, "type"); String type = cursor.getString(2); serializer.text(type); serializer.endTag(null, "type"); serializer.startTag(null, "body"); String body = cursor.getString(3); serializer.text(body); serializer.endTag(null, "body"); serializer.endTag(null, "sms"); fos.flush(); total++; //pd.setProgress(total); Thread.sleep(500); } serializer.endTag(null, "smss"); serializer.endDocument(); fos.flush(); fos.close(); }
时间: 2024-10-06 00:54:03