Reputation: 11
I am trying to append the notification received in the phone to a text file. But instead of appending its overwriting i.e. only the last notification is stored in the text file. I am also displaying the notifications on the app screen to verify the same.
My MainActivity.java is:
public class MainActivity extends Activity {
TableLayout tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = (TableLayout)findViewById(R.id.tab);
LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
}
private BroadcastReceiver onNotice= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String pack = intent.getStringExtra("package");
String title = intent.getStringExtra("title");
String text = intent.getStringExtra("text");
com.example.vk9621.notification1.Filehelper.generateNoteOnSD(getApplicationContext(),"Notification",pack,title,text);
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(getApplicationContext());
textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1.0f));
textview.setTextSize(20);
textview.setTextColor(Color.parseColor("#0B0719"));
textview.setText(Html.fromHtml(pack +"<br><b>" + title + " : </b>" + text));
tr.addView(textview);
tab.addView(tr);
}
};
}
My FileHelper.java is :
public class Filehelper {
public static void generateNoteOnSD(Context context, String sFileName, String sBody1,String sBody2, String sBody3) {
try {
File gpxfile;
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File file=new File(sFileName);
if(!file.exists()) {
gpxfile = new File(root, sFileName);
}
else{
gpxfile = file;
}
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody1);
writer.append(sBody2);
writer.append(sBody3);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
My NotificationService.java is :
public class NotificationService extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = sbn.getNotification().tickerText.toString();
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
Log.i("Package",pack);
Log.i("Ticker",ticker);
Log.i("Title",title);
Log.i("Text",text);
Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}
Can someone tell me whats the error and why all the notifications are not appearing in the text file?
Upvotes: 0
Views: 94
Reputation: 1427
May be you need to provide append parameter in FileWriter class like this,
FileWriter(File file, boolean append)
FileWriter writer = new FileWriter(gpxfile,true);
Call FileWriter constructor like this.
You can checkout this class in detail here
Upvotes: 3