【问题标题】:Android:Notification starts new activity instead of resuming old one if originally activity is not started from launcherAndroid:如果最初的活动不是从启动器启动的,通知会启动新的活动而不是恢复旧的活动
【发布时间】:2012-02-18 09:48:30
【问题描述】:

我花了一周的时间寻找解决方案,但没有成功,所以我需要帮助。

当我的应用程序进入后台时,会出现通知并显示活动状态。

如果应用程序从启动器启动(使用action = android.intent.action.MAINcategory = android.intent.category.LAUNCHER),它可以完美运行。 但是如果应用程序启动 f.e.从库中使用操作android.intent.action.SENDandroid.intent.action.SEND_MULTIPLE 和类别android.intent.category.DEFAULT,然后通知会启动新活动而不是恢复现有活动。

通知代码:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.icon;
Notification notification = new Notification(icon, null, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONLY_ALERT_ONCE;

Context context = this.getApplicationContext();
CharSequence contentTitle = "XXX";
CharSequence contentText = "XXX";

Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent activityIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, activityIntent);

notificationManager.notify(NOTIFICATION_ID, notification);

换句话说,我有一个带有活动“我的活动”的应用程序“我的应用程序”。如果“我的活动”在图库顶部启动并创建通知,则在按下通知后“我的应用程序”将启动,而不是使用“我的活动”恢复图库。

你知道如何治愈它吗?

【问题讨论】:

    标签: android android-activity android-intent notifications


    【解决方案1】:
        Intent myIntent = new Intent(this, MainActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
                PendingIntent pendingIntent = PendingIntent.getActivity(
                        getBaseContext(), 0, myIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT);
    
    
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notification.flags = notification.flags
                    | Notification.FLAG_ONGOING_EVENT;
            notification.flags |= Notification.FLAG_NO_CLEAR;
    

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多