praveen

Full sms backup

Oct 17th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. SMS -
  2.  
  3. public int getSms()
  4. {
  5. Uri smsuri = Uri.parse("content://sms/inbox");
  6.  
  7. int count =0;
  8. String file = smsuri.getHost();// + "-" + System.currentTimeMillis();
  9.  
  10. Cursor cursor = cr.query(smsuri, null, null, null, null);
  11. count = cursorToCSV(cursor,file);
  12. cursor.close();
  13.  
  14. String msg = String.format("Backed up %d records to %s file",count,file);
  15. Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
  16. return count;
  17. }
  18.  
  19.  
  20. cursorToCSV -
  21.  
  22. public int cursorToCSV(Cursor cursor, String file)
  23. {
  24. StringBuffer out = new StringBuffer();
  25.  
  26. int count=0;
  27.  
  28. // loop thru all the records
  29. while (cursor.moveToNext())
  30. {
  31. // loop thru all the columns
  32. for (int i = 0; i < cursor.getColumnCount(); i++)
  33. {
  34. String column = cursor.getString(i);
  35. if (i != 0)
  36. out.append(",");
  37. out.append(column); // adds a column
  38. }
  39. out.append("\n");
  40. count++;
  41. }
  42.  
  43. // save to a file
  44. try
  45. {
  46. FileOutputStream outStream = context.openFileOutput(file, Context.MODE_WORLD_WRITEABLE);
  47. outStream.write(out.toString().getBytes());
  48. outStream.close();
  49. Log.d("Backup", out.toString());
  50. } catch (IOException e)
  51. {
  52. Log.d("Backup", "Error writing CSV to file: "+file); e.printStackTrace();
  53. }
  54. return count;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment