Reputation: 1231
I'm trying to get content from a textview in another activity to show up in a notification bar message. It works, but not correctly. The string from the textview does show up in the notification, and I'm doing that by bundling the info from the other activities' textview and then have the notification manager grab the bundle. The problem arises when the other activity is launched, it fires off the notification because the last chunk of code in the activity does the bundling and sending, which causes the notification to fire, ignoring the set firing time. So my question is What's the best and easiest way to have a notification grab a string from another activity? Here's the activity, which is the problem. It fires the notification on it's own:
import java.io.IOException;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.widget.TextView;
public class DBTest2 extends Activity {
String scrNote;
TextView showBV;
NotificationManager nm;
DBAdapter dba;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dbtest_2);
showBV = (TextView) findViewById(R.id.getBK_TV);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//---cancel the notification---
try{
nm.cancel(getIntent().getExtras().getInt("uID"));
} catch (Exception e) {
System.out.println("Error when cancelling: "+e.toString());
}
//---END cancel the notification---
//---- SHOW IN NOTIFICATION------
scrNote = showBV.getText().toString();
Bundle moveScrNote = new Bundle();
moveScrNote.putString("mSN", scrNote);
Intent toNoteBody = new Intent(DBTest2.this, DisplayNotifications.class);
toNoteBody.putExtras(moveScrNote);
startActivity(toNoteBody);
//---- END SHOW IN NOTIFICATION------
}
}
and here is the notification manager:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---get the notification ID for the notification;
// passed in by the MainActivity---
int uID = getIntent().getExtras().getInt("uniqueID");
//---PendingIntent to launch activity
Intent noteI = new Intent("com.vee.search01.DBTEST2");
noteI.putExtra("uniqueID", uID);
PendingIntent herroIntent =
PendingIntent.getActivity(this, 0, noteI, 0);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
long fireTime = System.currentTimeMillis();
String noteTitle = "Notification Title";
Bundle getNoteBody = getIntent().getExtras();
String gotNoteBody = getNoteBody.getString("mSN");
String noteBody = gotNoteBody;
Notification note = new Notification(R.drawable.noteicon, noteTitle, fireTime);
note.setLatestEventInfo(this, noteTitle, noteBody, herroIntent);
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.FLAG_SHOW_LIGHTS;
nm.notify(uID, note);
finish();
}
}
Upvotes: 0
Views: 776
Reputation: 8641
The best way to transfer content between activities is to send it via extras in an Intent.
If you're issuing a notification from Activity A, and you want to handle it in Activity B, then create the notification in A and insert a PendingIntent containing an Intent to start B. When the notification displays and the user clicks it, B should be fired.
If you want to send the notification text from B to A, use a separate Intent.
If you're trying to send the text of the notification Intent to B as well as display the notification, put the text in the Intent's extras.
Also, if you're on a recent version of the platform, go read the reference docs for Notification. It's been deprecated in favor of creating notifications through Notification.Builder. One advantage is that you can set the notification to auto-cancel, so you don't have to cancel it in code.
Upvotes: 1