Reputation: 386
I have an application that reads incoming and outgoing sms. Logcat successfully show recieved and send messages. here is my code.
String[] columns = new String[]{"address", "date", "body", "type"};
String recipient = c.getString(c.getColumnIndex(columns[0]));
String date = c.getString(c.getColumnIndex(columns[1]));
String message = c.getString(c.getColumnIndex(columns[2]));
String type = c.getString(c.getColumnIndex(columns[3]));
Log.d("DetectOutgoingSMS", recipient + " , " + date + " , " + message + " , " +type);
Now I want to save all the above strings to a textfile. I tried below code to write it to text file.
try {
FileOutputStream fOut = openFileOutput("textfile.txt", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(message);
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Using this code I am unable to save all strings in one go. An when new message is recieved, previous is deleted from textfile and new messeage is inserted. Any help please.
Upvotes: 1
Views: 678
Reputation: 1433
Do you want to "save" the Strings, or "write" the Strings?
To "Save" the strings you need to use onPause()
or onSavedInstanceState()
. There are plenty of tutorials online to help you with that, depending on your goal.
To "Write" the Strings all at once you can use this.
String[] columns = new String[]{"address", "date", "body", "type"};
String recipient = c.getString(c.getColumnIndex(columns[0]));
String date = c.getString(c.getColumnIndex(columns[1]));
String message = c.getString(c.getColumnIndex(columns[2]));
String type = c.getString(c.getColumnIndex(columns[3]));
String all = ("OutgoingSMS: " + recipient + " , " + date + " , " + message + " , " +type);
Then replace this
osw.write(message);
with this
osw.write(all);
If you need anything else, or I misunderstood what you need, just ask
Upvotes: 0