Android知识点——TaskStackBuilder(类似于微信、QQ等点击通知栏)

场景:当应用处于后台时,默认情况下,从通知启动一个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);
}

相关阅读

全面了解Android Notification
参考

你可能感兴趣的:(Android)