Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SMS -
- public int getSms()
- {
- Uri smsuri = Uri.parse("content://sms/inbox");
- int count =0;
- String file = smsuri.getHost();// + "-" + System.currentTimeMillis();
- Cursor cursor = cr.query(smsuri, null, null, null, null);
- count = cursorToCSV(cursor,file);
- cursor.close();
- String msg = String.format("Backed up %d records to %s file",count,file);
- Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
- return count;
- }
- cursorToCSV -
- public int cursorToCSV(Cursor cursor, String file)
- {
- StringBuffer out = new StringBuffer();
- int count=0;
- // loop thru all the records
- while (cursor.moveToNext())
- {
- // loop thru all the columns
- for (int i = 0; i < cursor.getColumnCount(); i++)
- {
- String column = cursor.getString(i);
- if (i != 0)
- out.append(",");
- out.append(column); // adds a column
- }
- out.append("\n");
- count++;
- }
- // save to a file
- try
- {
- FileOutputStream outStream = context.openFileOutput(file, Context.MODE_WORLD_WRITEABLE);
- outStream.write(out.toString().getBytes());
- outStream.close();
- Log.d("Backup", out.toString());
- } catch (IOException e)
- {
- Log.d("Backup", "Error writing CSV to file: "+file); e.printStackTrace();
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment