Android官方开发文档Training系列课程中文版:通知用户之创建不同导航方式的Activity
生活随笔
收集整理的這篇文章主要介紹了
Android官方开发文档Training系列课程中文版:通知用户之创建不同导航方式的Activity
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原文地址:http://android.xsoftlab.net/training/notify-user/navigation.html
設計通知時要考慮到用戶所預想的導航體驗。通常有以下兩種情況:
常規(guī)的Activity(Regular activity)
- 這里所啟動的Activity是作為應用程序的正常流程部分出現(xiàn)的。
指定的Activity(Special activity)
- 用戶只會看到這個Activity,如果這個Activity是從通知啟動的話。在直覺上,這個Activity是用來展示通知上的詳細信息的。
設置常規(guī)的Activity
設置啟動常規(guī)的Activity需要執(zhí)行以下步驟:
- 1.在清單文件中定義Activity的層級,最終的清單文件應該是這樣的:
- 2.創(chuàng)建一個基于回退棧的Intent,它用來啟動父Activity(下面的代碼可能有誤,請自行驗證。):
設置啟動指定的Activity
啟動指定的Activity不需要回退棧,所以不需要在清單文件中定義Activity的層級,也不需要在代碼中使用addParentStack()構建回退棧。相反的,使用清單文件來設置Activity的任務模式,并通過getActivity()創(chuàng)建PendingIntent就可以完成創(chuàng)建。
1.在清單文件中,為Activity添加如下屬性:
- android:name=”activityclass” 指定該Activity的全限定名稱.
- android:taskAffinity=”” 與在代碼中設置的FLAG_ACTIVITY_NEW_TASK標志一同使用。這可以確保這個Activity不會進入應用程序的默認任務棧中。任何已有的任務棧皆不會受到這個屬性的影響。
- android:excludeFromRecents=”true” 將該Activity從Recents中排除,所以用戶不會意外的再通過返回鍵啟動這個Activity.
下面的代碼段展示了這個屬性的設置:
<activity android:name=".ResultActivity" ...android:launchMode="singleTask"android:taskAffinity=""android:excludeFromRecents="true"> </activity> ...- 2.構建及發(fā)射通知
- a.創(chuàng)建一個用于啟動Activity的Intent.
- b.通過調用setFlags()方法并設置FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK標志來設置Activity將要啟動一個新的、空的任務棧.
- c.為Intent設置你所需要的選項.
- d.通過調用getActivity()由Intent創(chuàng)建一個PendingIntent。你可以將這個PendingIntent作為setContentIntent()方法的參數(shù)。
下面的代碼段演示了這個實現(xiàn)過程:
// Instantiate a Builder object. NotificationCompat.Builder builder = new NotificationCompat.Builder(this); // Creates an Intent for the Activity Intent notifyIntent =new Intent(new ComponentName(this, ResultActivity.class)); // Sets the Activity to start in a new, empty task notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Creates the PendingIntent PendingIntent notifyIntent =PendingIntent.getActivity(this,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT ); // Puts the PendingIntent into the notification builder builder.setContentIntent(notifyIntent); // Notifications are 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 NotificationManager mNotificationManager.notify(id, builder.build());總結
以上是生活随笔為你收集整理的Android官方开发文档Training系列课程中文版:通知用户之创建不同导航方式的Activity的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习算法与自然语言处理
- 下一篇: Android官方开发文档Trainin