Reputation: 155
I have set up a Broadcast Receiver to capture current running task. The Class display the task name using Toast but I need to write the String to a file
Is there a way I can write the String to a file within the Broadcast Receiver?
public void onReceive(Context aContext, Intent anIntent) {
try {
//TextView run = (TextView)findViewById(R.id.text1);
//ArrayList<String> task1 = new ArrayList<String>();
ActivityManager am = (ActivityManager) aContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> alltasks = am
.getRunningTasks(1);
for (ActivityManager.RunningTaskInfo aTask : alltasks) {
task=aTask.baseActivity.getPackageName();
//task1.add(task);
// run.setText(task);
Toast.makeText(aContext, task , Toast.LENGTH_LONG).show();
saveTask(aContext,task);
}
Now I want to write the string task into a file. How to do it within broadcast receiver class?
Upvotes: 1
Views: 548
Reputation: 29642
Following is a simple code to write a text in file.
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append( "Your Text goes here");
myOutWriter.close();
fOut.close();
Also you need to define following permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 1