Android Official Development Document Training Series course in Chinese: Notify users to create different navigation modes of activity

Source: Internet
Author: User

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.

    • 1. In the manifest file, add the following properties to the activity:

      • Android:name= "Activityclass" specifies the fully qualified name of the activity.
      • Android:taskaffinity= "" is used in conjunction with the FLAG_ACTIVITY_NEW_TASK flag set in the code. This ensures that the activity does not go into the application's default task stack. Any existing task stacks are unaffected by this attribute.
      • Android:excludefromrecents= "True" excludes the activity from recents , so the user does not accidentally start the activity with a return key.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.