W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
編寫:fastcome1985 - 原文:http://developer.android.com/training/notify-user/navigation.html
部分設(shè)計(jì)一個(gè)notification的目的是為了保持用戶的導(dǎo)航體驗(yàn)。為了詳細(xì)討論這個(gè)課題,請(qǐng)看 Notifications API引導(dǎo),分為下列兩種主要情況:
* 常規(guī)的activity
你啟動(dòng)的是你application工作流中的一部分[Activity](developer.android.com/reference/android/app/Activity.html)。
* 特定的activity
用戶只能從notification中啟動(dòng),才能看到這個(gè)[Activity](http://developer.android.com/intl/zh-cn/reference/android/app/Activity.html),在某種意義上,這個(gè)[Activity](http://developer.android.com/intl/zh-cn/reference/android/app/Activity.html)是notification的擴(kuò)展,額外展示了一些notification本身難以展示的信息。
設(shè)置一個(gè)直接啟動(dòng)的入口Activity的PendingIntent,遵循以下步驟:
1 在manifest中定義你application的Activity層次,最終的manifest文件應(yīng)該像這個(gè):
<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 在基于啟動(dòng)Activity的Intent中創(chuàng)建一個(gè)返回棧,比如:
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());
一個(gè)特定的Activity不需要一個(gè)返回棧,所以你不需要在manifest中定義Activity的層次,以及你不需要調(diào)用 addParentStack())方法去構(gòu)建一個(gè)返回棧。作為代替,你需要用manifest設(shè)置Activity任務(wù)選項(xiàng),以及調(diào)用 getActivity())創(chuàng)建PendingIntent
manifest中,在Activity的 標(biāo)簽中增加下列屬性: android:name="activityclass" activity的完整的類名。android:taskAffinity="" 結(jié)合你在代碼里設(shè)置的FLAG_ACTIVITY_NEW_TASK標(biāo)識(shí), 確保這個(gè)Activity不會(huì)進(jìn)入application的默認(rèn)任務(wù)。任何與 application的默認(rèn)任務(wù)有密切關(guān)系的任務(wù)都不會(huì)受到影響。android:excludeFromRecents="true" 將新任務(wù)從最近列表中排除,目的是為了防止用戶不小心返回到它。
建立以及發(fā)布notification: a.創(chuàng)建一個(gè)啟動(dòng)Activity的Intent. b.通過調(diào)用setFlags())方法并設(shè)置標(biāo)識(shí)FLAG_ACTIVITY_NEW_TASK 與 FLAG_ACTIVITY_CLEAR_TASK,來設(shè)置Activity在一個(gè)新的,空的任務(wù)中啟動(dòng)。 c.在Intent中設(shè)置其他你需要的選項(xiàng)。 d.通過調(diào)用 getActivity()方法從Intent中創(chuàng)建一個(gè) PendingIntent,你可以把這個(gè)PendingIntent 當(dāng)做 setContentIntent()的參數(shù)來使用。 下面的代碼片段演示了這個(gè)過程:
// 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());
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: