日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android官方开发文档Training系列课程中文版:通知用户之创建不同导航方式的Activity

發(fā)布時間:2024/7/5 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的層級,最終的清單文件應該是這樣的:
<activity android: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> <activity android:name=".ResultActivity"android:parentActivityName=".MainActivity"><meta-data android:name="android.support.PARENT_ACTIVITY"android:value=".MainActivity"/> </activity>
  • 2.創(chuàng)建一個基于回退棧的Intent,它用來啟動父Activity(下面的代碼可能有誤,請自行驗證。):
int id = 1; ... Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent to the top of the stack stackBuilder.addNextIntent(resultIntent); // Gets a PendingIntent containing the entire back stack PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); ... NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(id, builder.build());

設置啟動指定的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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。