Android 4.1 (Jelly Bean) 增强了通知功能,在AndroidManifest.xml中可以为Activity添加属性android:parentActivityName="MainActivity",实现点击通知打开ResultActivity时,再按返回键,回到MainActivity,而不是回到之前的Task。只在4.1的机器上有效,在2.x的机器上和以前没有任何区别。

核心代码如下:

    public void showNotification() {    NotificationCompat.Builder mBuilder =      new NotificationCompat.Builder(this)      .setSmallIcon(R.drawable.ic_launcher)      .setContentTitle("My Notification")      .setContentText("Hello World");        //NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();//    String [] events = new String[6];//    inboxStyle.setBigContentTitle("Event tracker details:");//    for (String event : events) {//    inboxStyle.addLine(event);//    }//    mBuilder.setStyle(inboxStyle);        Intent intent = new Intent(this, ResultActivity.class);        //The stack builder object will contain an artificial back stack for the started Activity.    //This ensures that navigating backward from the Activity leads out of your application to the Home screen    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);        //Adds the back stack for the Intent (but not the Intent itself)    stackBuilder.addParentStack(ResultActivity.class);        //Adds the intent that starts the Activity to the top of the stack    stackBuilder.addNextIntent(intent);        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);    mBuilder.setContentIntent(pendingIntent);        mBuilder.setDefaults(Notification.DEFAULT_SOUND);    mBuilder.setAutoCancel(true);    //mBuilder.setDefaults(Notification.DEFAULT_ALL);        NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    mgr.notify(0, mBuilder.build());    }


部分AndroidManifiest.xml代码
<activity android:name=".ResultActivity"            android:parentActivityName=".MainActivity" ></activity>


参考:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

更多相关文章

  1. Android内嵌H5(2)
  2. Android(安卓)技术要点
  3. Android简明开发教程十六:Button 画刷示例
  4. Android(安卓)技术要点
  5. Android(安卓)Camera使用小结
  6. Android(安卓)浏览器的开发实例分享
  7. android jni 程序框架搭建
  8. Android(安卓)Camera使用小结
  9. android全屏,去掉title栏的办法

随机推荐

  1. 基于svelteKit开发仿微信app界面聊天实例
  2. laravel解决超卖的几个方法
  3. 建议自查!MySQL驱动Bug引发的事务不回滚问
  4. 面试中的最常被问到的两种锁
  5. 【Java面试】@Resource 和 @Autowired 的
  6. 新手必会的静态站点生成器——Gridsome
  7. 后台用户列表数据展示(php)
  8. 如何学插画?学习插画教程
  9. 【知其然,知其所以然】配置中心 Apollo源
  10. 属性与方法重载 、命名空间与类自动加载