Original address: http://android.xsoftlab.net/training/notify-user/navigation.html
Design notifications to take into account the user's desired navigation experience. There are usually two cases:
Regular activity (Regular activity)
- The activity that is initiated here appears as part of the normal process of the application.
Specified activity (special activity)
- The user will only see this activity if the activity is initiated from the notification. Intuitively, this activity is used to show the details on the notification.
Set up a regular activity
Setting up a regular activity requires the following steps:
- 1. Define the level of activity in the manifest file, and the final manifest file should look like this:
<activityandroid:name=". Mainactivity "android:label=" @string/app_name " > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter></activity><activityandroid:name=". Resultactivity "android:parentactivityname=". Mainactivity "> <meta-dataandroid:name="Android.support.PARENT_ACTIVITY"android:value =". Mainactivity "/> </activity>
- 2. Create a fallback stack-based intent that is used to start the parent activity (the following code may be wrong, please verify it yourself.) ):
intID =1;... Intent resultintent =NewIntent ( This, Resultactivity.class); Taskstackbuilder Stackbuilder = Taskstackbuilder.create ( This);//Adds the back stackStackbuilder.addparentstack (Resultactivity.class);//Adds the Intent to the top of the stackStackbuilder.addnextintent (resultintent);//Gets a pendingintent containing the entire back stackPendingintent resultpendingintent = stackbuilder.getpendingintent (0, pendingintent.flag_update_current);.. Notificationcompat.builder Builder =NewNotificationcompat.builder ( This); Builder.setcontentintent (resultpendingintent); Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Context.notification_service); Mnotificationmanager.notify (ID, builder.build ());
Set launch specified activity
Starting the specified activity does not require a fallback stack, so you do not need to define the level of activity in the manifest file or use Addparentstack () in your code to build the fallback stack. Instead, use the manifest file to set the activity's task pattern and create the pendingintent with Getactivity () to complete the creation.
The following code snippet shows the settings for this property:
< Activity android:name = ". Resultactivity " android:launchmode = "Singletask" android:taskaffinity = =; < /activity ; ...
- 2. Build and launch notifications
- A. Create a intent to start the activity.
- B. Set the ACTIVITY to start a new, empty task stack by calling the SetFlags () method and setting the Flag_activity_new_task and Flag_activity_clear_task flags.
- C. Set the options you need for intent.
- D. Create a pendingintent from intent by calling Getactivity (). You can use this pendingintent as a parameter to the Setcontentintent () method.
The following code snippet illustrates this implementation process:
//Instantiate a Builder object.Notificationcompat.builder Builder =NewNotificationcompat.builder ( This);//Creates an Intent for the ActivityIntent notifyintent =NewIntent (NewComponentName ( This, Resultactivity.class));//sets the Activity to start in a new, empty taskNotifyintent.setflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task);//creates the PendingintentPendingintent notifyintent = pendingintent.getactivity ( This,0, Notifyintent, pendingintent.flag_update_current);//Puts the pendingintent into the notification builderBuilder.setcontentintent (notifyintent);//Notifications is issued by sending them to the//Notificationmanager system service.Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Context.notification_service);//Builds an anonymous Notification object from the builder, and//Passes it to the NotificationmanagerMnotificationmanager.notify (ID, builder.build ());
Android Official Development Document Training Series course in Chinese: Notify users to create different navigation modes of activity