The previous article on how to create and display a notification, this article summarizes the click Notification after the program should respond.
In general, after clicking on a notification, an activity is opened as a response to the Click event, which was previously set in the Pendingintent.
Often play Android phone should have the impression that in the Calendar app, you create a new reminder, when the reminder notification received, you click on the notification, will go to the content page of the reminder, if this time press the back key, will exit the application directly.
However, in the Gmail application, if there is a new mail arrives, then click on the notification, will go to the content page of the mail, wait for you to read the mail, click the Back button, will be returned to the mailing list page, and then press the back key, will quit the app.
We summarize the two scenarios, assuming that our application has two activity (parentactivity, subactivity), notification set open activity as subactivity.
So the first scenario is:
Click notification--> to enter subactivity ---back--- exit the app
Second case:
Click notification--> to enter subactivity ---- back to parentactivity -->back and exit the app
The first case is relatively simple, only need to specify activity in the Pendingintent, no other settings are required, Android is the default.
Pendingintent contentintent = pendingintent.getactivity (context, 0, intent, pendingintent.flag_cancel_current);
However, you need to be aware of the parameters when creating Pendingintent pendingintent.flag_cancel_current
This flag is used to indicate that if the current activity is the same as the intent set in the pendingintent, the current activity will be canceled for a long time and replaced with the activity specified in the pendingintent.
In addition, you need to set properties on the specified activity in manifest
<activity android:name= ". Subactivityl "android:launchmode=" Singletask "android:taskaffinity=" "android:excludefromrecent S= "true" > </activity>
The second situation is slightly more complicated, because if you open only one subactivity, the program doesn't know who he is at the top level of activity, so you need to open a group of activity when you click Notification. But we do not need to call the StartActivity method, Pendingintent provides a static method Getactivities, which can set a intent array to specify a series of activity.
So we first write a function to create an activity array:
Intent[] Makeintentstack (context context) {intent[] intents = new INTENT[2]; Intents[0] = Intent.makerestartactivitytask (new componentname (context, Com.example.notificationtest.MainActivity.class)); INTENTS[1] = new Intent (context, com.example.notificationtest.SubActivity.class); return intents; }
One of the things to note is the Intent.makerestartactivitytask method, which is used to create the activity stack's root activity
Next, create and display the notification:
Void shownotification (intent intent) { Notification Notification = new notification ( R.drawable.status_icon, "Hello world ticker text", system.currenttimemillis ()); pendingintent contentintent = pendingintent.getactivities ( this, 0, makeintentstack (This), pendingintent.flag_cancel_current); notification.setlatesteventinfo ( this, "Title", "hey, shall we have a dinner Tonight ", Contentintent); notification.flags |= notification.default_all; mnm.notify (1, notification); }
Android Notification--Respond to notification events