Android知識點——TaskStackBuilder

小編發表於2017-02-21

積跬步,以至千里;積小流,以成江海。

場景:當應用處於後臺時,預設情況下,從通知啟動一個Activity,按返回鍵會回到主螢幕。但遇到這樣的需求,按返回鍵時仍然留在當前應用。類似於微信、QQ等點選通知欄,顯示Chat頁,點選返回會回到主Activity。

在MainActivity點選按鈕開啟一個服務,並將Activity退出。服務中子執行緒睡眠3秒後,模擬彈出通知。點選通知欄,進入訊息列表頁後。點選返回按鈕時,可見直接回到了桌面。並沒有回到自己主頁面的。

Android知識點——TaskStackBuilder
預設形式

程式碼片段:

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);
}複製程式碼

Android知識點——TaskStackBuilder
點選返回,回到主介面

實現上述需求,採用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的關係即可。打包的時候會合並清單檔案配置。

相關閱讀

全面瞭解Android Notification

相關文章