我們總結一下兩種情況,假設我們的應用有兩個Activity(ParentActivity叨叙、SubActivity),notification中設置打開的Activity為SubActivity。
第一種情況就是:
點擊Notification ——>進入SubActivity ——> back鍵 ——> 退出應用
第二種情況:
點擊Notification ——>進入SubActivity ——> back鍵 ——> 退到ParentActivity ——>back鍵 ——>退出應用
第一種情況
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
***注意最后一個參數(shù)***
FLAG_CANCEL_CURRENT:如果構建的PendingIntent已經存在耘戚,則取消前一個洽瞬,重新構建一個本涕。(PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 第二個參數(shù)是0 )
FLAG_NO_CREATE:如果前一個PendingIntent已經不存在了,將不再構建它伙窃。
FLAG_ONE_SHOT:表明這里構建的PendingIntent只能使用一次菩颖。
FLAG_UPDATE_CURRENT:如果構建的PendingIntent已經存在,那么 系統(tǒng)將不會重復創(chuàng)建为障,只是把之前不同的傳值替換掉晦闰。(所以這里如果值不變 是不是有變化的 以致于會出現(xiàn)一些問題 通常做法就是在構建PendingIntent的時候傳入不一樣的requestCode來改變 更新PendingIntent 一般都是用這個放祟;PendingIntent contentIntent = PendingIntent.getActivity(context, new Random().nextInt() , intent, PendingIntent.FLAG_CANCEL_CURRENT); 第二個參數(shù)是隨機數(shù) )
***activity要實現(xiàn)單例模式***
android:launchMode="singleTask"
第二種情況:
因為如果只打開一個SubActivity,程序并沒辦法知道他的上一級Activity是誰呻右,所以需要在點擊Notification時打開一組Activity舞竿,但是我們并不需要一個個去調用startActivity方法,PendingIntent提供了個靜態(tài)方法getActivities窿冯,里面可以設置一個Intent數(shù)組骗奖,用來指定一系列的Activity。
所以我們首先寫一個函數(shù)創(chuàng)建一個Activity數(shù)組:
Intent[] makeIntentStack(Context context) {
Intent[] intents = new Intent[2];
intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, com.example.notificationtest.MainActivity.class));
intents[1] = new Intent(context, com.example.notificationtest.SubActivity.class);
return intents;
}
其中需要注意的是Intent.makeRestartActivityTask方法醒串,這個方法用來創(chuàng)建activity棧的根activity
接下來执桌,創(chuàng)建并顯示Notification:
void showNotification(Intent intent) {
Notification notification = new Notification(
R.drawable.status_icon,
"Hello World ticker text",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivities(
this,
0,
makeIntentStack(this),
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(
this,
"Title",
"Hey, shall we have a dinner tonight",
contentIntent);
notification.flags |= Notification.DEFAULT_ALL;
mNM.notify(1, notification);
}
完整實例:
// 創(chuàng)建一個NotificationManager的引用
NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定義Notification的各種屬性
Notification notification = new Notification(R.drawable.icon, "測試", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 該通知能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_NO_CLEAR 該通知不能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_ONGOING_EVENT 通知放置在正在運行
//FLAG_INSISTENT 是否一直進行,比如音樂一直播放芜赌,知道用戶響應
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 將此通知放到通知欄的"Ongoing"即"正在運行"組中
notification.flags |= Notification.FLAG_NO_CLEAR;
// 表明在點擊了通知欄中的"清除通知"后仰挣,此通知不清除,經常與FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默認值缠沈,比如聲音膘壶,震動,閃屏等等
//DEFAULT_LIGHTS 使用默認閃光提示
//DEFAULT_SOUNDS 使用默認提示聲音
//DEFAULT_VIBRATE 使用默認手機震動洲愤,需加上<uses-permission android:name="android.permission.VIBRATE" />權限
notification.defaults = Notification.DEFAULT_LIGHTS;
//疊加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000; //閃光時間颓芭,毫秒
// 設置通知的事件消息
CharSequence contentTitle = "標題"; // 通知欄標題
CharSequence contentText = "內容"; // 通知欄內容
//如果需要跳轉到指定的Activity,則需要設置PendingIntent
Intent notificationIntent = new Intent(A.this, B.class);
// 點擊該通知后要跳轉的Activity
notificationIntent.putExtra("date", "需要傳遞的參數(shù)");
// FLAG_UPDATE_CURRENT 更新數(shù)據(jù)柬赐,如果有多個PendingIntent亡问,且requestCode相同,則會替換為最新extra數(shù)據(jù)
//如果需要通過不同的extra數(shù)據(jù)肛宋,進行處理州藕,就需要requestCode不相同
int requestCode = new Random().nextInt();
PendingIntent contentItent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
// 把Notification傳遞給NotificationManager
notificationManager.notify(0, notification);