@Override public void run() { // Needed to close the ProgressDialog. Looper.prepare(); try { // example file name: sms_backup_2012-05-23_133700.xml String dateStr = DateFormat.format("yyyy-MM-dd_hhmmss", System.currentTimeMillis()).toString(); File backupFile = new File(getExternalFilesDir(null), "sms_backup_" + dateStr + ".xml"); OutputStream out = new FileOutputStream(backupFile); SmsWriter backupCreator = new XmlSmsWriter(); backupCreator.setOutputstream(out); backupCreator.startBackup(); backupSms(SmsFolder.INBOX, backupCreator); backupSms(SmsFolder.OUTBOX, backupCreator); backupCreator.finishBackup(); out.close(); handler.sendEmptyMessage(HANDLER_WHAT_SUCCESSFULL); } catch (IOException e) { handler.sendEmptyMessage(HANDLER_WHAT_ERROR_MESSAGE); Log.e(TAG, getString(R.string.error_cant_write_file), e); } finally { handler.sendEmptyMessage(HANDLER_WHAT_CLOSE_PROGRESS_DIALOG); Looper.myLooper().quit(); } }
/** * Reads all text messages from the given folder and create for each a record in the {@link * SmsWriter}. * * @param folder The message folder. * @param creator the creator * @throws IOException */ private void backupSms(final SmsFolder folder, final SmsWriter writer) throws IOException { Cursor c = smsAccess.getFolder(folder); if (c != null && c.moveToFirst()) { Log.d(TAG, "Starting backup"); do { String address = c.getString(c.getColumnIndex("address")); String body = c.getString(c.getColumnIndex("body")); String date = c.getString(c.getColumnIndex("date")); writer.writeRecord(address, date, folder.name(), body); } while (c.moveToNext()); Log.d(TAG, "Finishing backup"); } c.close(); }