i’m try to develop an app for organizing . so after setting an task for particular time, i created a notification, so this notification displays on the status bar and when the user touch on this, current activity will appear.
My problem is : when the notification appear, the activity is launched also.
there is anyways to prevent this ? i mean the activity don’t start automatically when the notification appear, and the user have to click on the notification to wake the activity up.
I have class “Main” and an alarm manager inside. this alarm manager will fire at the particular time and start an another activity (DisplayNotification).
{
Intent intent = new Intent("com.test.DisplayNotification");
intent.putExtra("alarm_message", "Alarm");
intent.putExtra("item_name", "message");
PendingIntent broadcast = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mili, broadcast);
}
In the DisplayNotification class i have
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String message = getIntent().getExtras().getString("item_name");
Intent i = new Intent("com.test.Main");
PendingIntent detailsIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.icon,
"Time's up!",
System.currentTimeMillis());
CharSequence from = "AlarmManager - Time's up!";
notif.setLatestEventInfo(this, from, message, detailsIntent);
nm.notify(1, notif);
//---destroy the activity---
finish();
}
Use a
BroadcastReceiverto ‘listen’ for the result of the alarm and then have it create theNotificationin itsonReceive(...)method.