技术文章阅读笔记

Notification中使用TaskStackBuilder类

2017-02-03  本文已影响51人  ahking17

java代码:

        mBuilder = new NotificationCompat.Builder(this).setContent(view)
                .setSmallIcon(R.drawable.icon).setTicker("新资讯")
                .setWhen(System.currentTimeMillis())
                .setOngoing(false)
                .setAutoCancel(true);
        Intent intent = new Intent(this, NotificationShow.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(NotificationShow.class);
        stackBuilder.addNextIntent(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);

AndroidManifest.xml中设置android:parentActivityName属性值.

        <activity
            android:name="com.shulf.notificationtest.NotificationShow"
            android:parentActivityName=".MainActivity" >
        </activity>

这样, 当通知栏被点击后, 显示NotificationShow Activity, 当点击back时, 不会回到桌面, 而是进入app的主页面.

之前要想达到同样目的, 需要在Activity中记录一个flag值, 用来标记这个Activity是从notification打开的, 然后在Activity的 onBackPressed()中做处理导航进入MainActivity.

----DONE.---------

上一篇下一篇

猜你喜欢

热点阅读