積跬步,以至千里;積小流,以成江海。
場景:當應用處于后臺時,默認情況下,從通知啟動一個Activity,按返回鍵會回到主屏幕。但遇到這樣的需求,按返回鍵時仍然留在當前應用。類似于微信、QQ等點擊通知欄,顯示Chat頁,點擊返回會回到主Activity。
一
在MainActivity點擊按鈕開啟一個服務,并將Activity退出。服務中子線程睡眠3秒后,模擬彈出通知。點擊通知欄,進入消息列表頁后。點擊返回按鈕時,可見直接回到了桌面。并沒有回到自己主頁面的。
默認形式
代碼片段:
private void showNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent msgIntent = new Intent();
msgIntent.setClass(this, MessageActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, msgIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// create and send notificaiton
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSmallIcon(getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)//自己維護通知的消失
.setContentTitle("我是標題")
.setTicker("我是ticker")
.setContentText("我是內容")
.setContentIntent(pendingIntent);
//將一個Notification變成懸掛式Notification
mBuilder.setFullScreenIntent(pendingIntent, true);
Notification notification = mBuilder.build();
manager.notify(0, notification);
}
二
實現上述需求,采用PendingIntent.getActivities()方法
代碼片段:
private void showNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent msgIntent = new Intent();
Intent mainIntent = new Intent();
msgIntent.setClass(this, MessageActivity.class);
mainIntent.setClass(this, MainActivity.class);
//注意此處的順序
Intent[] intents = new Intent[]{mainIntent, msgIntent};
PendingIntent pendingIntent = PendingIntent.
getActivities(this, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT);
// create and send notificaiton
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSmallIcon(getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)//自己維護通知的消失
.setContentTitle("我是標題")
.setTicker("我是ticker")
.setContentText("我是內容")
.setContentIntent(pendingIntent);
//將一個Notification變成懸掛式Notification
mBuilder.setFullScreenIntent(pendingIntent, true);
Notification notification = mBuilder.build();
manager.notify(0, notification);
}
點擊返回,回到主界面
三
實現上述需求,采用TaskStackBuilder方法
1.在AndroidManifest.xml配置Activity關系
<activity android:name=".MessageActivity"
android:parentActivityName=".MainActivity"/>
2.代碼
private void showNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//啟動通知Activity時,拉起主頁面Activity
Intent msgIntent = new Intent();
msgIntent.setClass(this, MessageActivity.class);
//創建返回棧
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//添加Activity到返回棧
stackBuilder.addParentStack(MessageActivity.class);
//添加Intent到棧頂
stackBuilder.addNextIntent(msgIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// create and send notificaiton
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSmallIcon(getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)//自己維護通知的消失
.setContentTitle("我是標題")
.setTicker("我是ticker")
.setContentText("我是內容")
.setContentIntent(pendingIntent);
//將一個Notification變成懸掛式Notification
mBuilder.setFullScreenIntent(pendingIntent, true);
Notification notification = mBuilder.build();
manager.notify(0, notification);
}
小結:
第二種方式適合項目在一個module中開發的情況。如果是組件化開發,通知頁面MessageActivity在其他module中,則是無法引用到MainActivity的。因此采用第三種方式更合適。只需要在app的清單文件中再次配置一下Activity的關系即可。打包的時候會合并清單文件配置。