TaskStackBuilder
来源:互联网 发布:淘宝微淘广播怎么发布 编辑:程序博客网 时间:2024/06/06 09:32
默认情况下,从通知启动一个Activity,按返回键会回到主屏幕。但遇到这样的需求,按返回键时仍然留在当前应用。类似于微信、QQ等点击通知栏,显示Chat页,点击返回会回到主Activity
实现方式一:
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);}
总结:
第二种方式适合项目在一个module中开发的情况。如果是组件化开发,通知页面MessageActivity在其他module中,则是无法引用到MainActivity的。因此采用第三种方式更合适。只需要在app的清单文件中再次配置一下Activity的关系即可。打包的时候会合并清单文件配置。
阅读全文
0 0
- TaskStackBuilder
- TaskStackBuilder使用
- Notification中使用TaskStackBuilder
- Android知识点——TaskStackBuilder
- TaskStackBuilder 不支持Android 3.0以下的设备
- TaskStackBuilder and extras for the back stack
- Android 一次启动多个Activity (TaskStackBuilder)
- The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.
- 安卓报错: The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.
- SlidingMenu接入时提示The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolve
- The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved. It is indirec
- The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved. It is indirec
- Mockito中java.lang.noclassdeffounderror android/support/v4/app/taskstackbuilder$supportparentable
- Android知识点——TaskStackBuilder(类似于微信、QQ等点击通知栏)
- 安卓开发错误:The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.
- TaskStackBuilder$SupportParentable cannot be resolved. It is indirectly referenced from required .cl
- vue-devtools chrome 开发工具
- 事件与动画
- Dubbo框架搭建通用接口
- vim插入命令
- [Unity UGUI]点击和长按组件
- TaskStackBuilder
- 英特尔Skylake处理器全面入驻Google Compute Engine
- 实习随手记-文件系列操作
- JAVA字符串格式化-String.format()的使用
- mysql读写分离-mysql proxy实现
- 欢迎使用CSDN-markdown编辑器
- HDFS下断点续传的实现——下载
- js实现pc浏览器复制粘贴功能
- Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面