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线程

相关文章
相关标签/搜索