Android知識點——TaskStackBuilder

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

場景:當應用處於後臺時,默認狀況下,從通知啓動一個Activity,按返回鍵會回到主屏幕。但遇到這樣的需求,按返回鍵時仍然留在當前應用。相似於微信、QQ等點擊通知欄,顯示Chat頁,點擊返回會回到主Activity。android

在MainActivity點擊按鈕開啓一個服務,並將Activity退出。服務中子線程睡眠3秒後,模擬彈出通知。點擊通知欄,進入消息列表頁後。點擊返回按鈕時,可見直接回到了桌面。並無回到本身主頁面的。微信

默認形式

代碼片斷:app

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關係ui

<activity android:name=".MessageActivity" android:parentActivityName=".MainActivity"/>複製代碼

2.代碼this

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的關係便可。打包的時候會合並清單文件配置。spa

相關閱讀

全面瞭解Android Notification線程

相關文章
相關標籤/搜索