Android 点击通知栏图标 返回已经运行的程序,而不开启一个新的Activity

来源:互联网 发布:php while 编辑:程序博客网 时间:2024/05/23 19:01

很多网上关于 通知栏的例子都是打开一个新的Activity,代码也很多。

如果要实现点击通知图标返回已经运行的程序,我只贴出关键的部分代码。

public void showNotify() {Notification notification = new Notification(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notification.icon = R.drawable.appicon; //你自己的资源图片Context mContext = getApplicationContext();Intent appIntent = new Intent(Intent.ACTION_MAIN);appIntent.addCategory(Intent.CATEGORY_LAUNCHER);appIntent.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName()));appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式PendingIntent intent = PendingIntent.getActivity(mContext, 0,appIntent, 0);notification.setLatestEventInfo(getApplicationContext(),getString(R.string.app_name), "", intent);notificationManager.notify(0, notification);}


     在真测试时会遇到一个问题:当你安装包在手机上安装时(不是连接在电脑上用Run As运行的), 安装完成后点击 【打开】按钮进入程序后,再点击通知栏上的图标,就会导致运行一个新的Activity,而且原来运行的Activity也在运行。

    这种问题只会出现在第一次安装后立即运行时,但是覆盖安装并不会有这样的情况。我也还找不到办法解决,如果有朋友解决了 分享下。