【问题标题】:Android Notification restarts app but want to resumeAndroid Notification 重新启动应用程序但想要恢复
【发布时间】:2011-08-02 02:09:24
【问题描述】:

您好,我已经能够获得为我的活动显示的通知,并且当用户 单击应用程序重新启动的通知。但是我只是希望它重新出现而不是重新启动。例如。它是一个网络应用程序,我希望它在用户选择通知时出现在前面。但不刷新网页。我可以捕获这个意图还是我发送了错误的意图?通常,如果我按下主页按钮并单击应用程序图标,应用程序就会出现并且不会刷新/重新启动。所以这就是我想要的行为。有什么想法吗?

 String ns = Context.NOTIFICATION_SERVICE;
 NotificationManager mNotificationManager = (NotificationManager)
                                             getSystemService(ns);
 //2.Instantiate the Notification
 int icon = R.drawable.notification_icon;
 CharSequence tickerText = "My App";  // temp msg on status line 
 long when = System.currentTimeMillis();
 Notification notification = new Notification(icon, tickerText, when);
 notification.flags |= Notification.FLAG_ONGOING_EVENT;
 //3.Define the Notification's expanded message and Intent: 
 Context context = getApplicationContext();
 CharSequence contentTitle = "Notification";
 CharSequence contentText = "My app!"; // message to user
 Intent notificationIntent = new Intent(this, HelloAndroid2.class);
 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
                                                          notificationIntent, 0);
 notification.setLatestEventInfo(context, contentTitle, contentText,
                                                          contentIntent);
 //4.Pass the Notification to the NotificationManager:  
 final int NOTIFICATION_ICON_ID = 1;
 mNotificationManager.notify(NOTIFICATION_ICON_ID, notification);

【问题讨论】:

    标签: android background notifications foreground android-pendingintent


    【解决方案1】:

    我把它放在清单中

    android:launchMode="singleInstance"
    

    这解决了我的问题。还有this answer,我没有尝试过,它暗示了其他有趣的事情。

    【讨论】:

    • 此属性是否引起了其他问题?谢谢
    【解决方案2】:

    如果您必须避免将活动设置为“singleInstance”,则将通知的意图设置如下:

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    【讨论】:

      【解决方案3】:

      你可以试试这个 FLAG_ACTIVITY_REORDER_TO_FRONT(文档描述的正是你想要的)

      http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

      Intent notificationIntent = new Intent(this, HelloAndroid2.class);
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      

      【讨论】:

      【解决方案4】:

      我建议为这种特殊情况设置标志 Intent.FLAG_ACTIVITY_SINGLE_TOP,如果您使用 android:launchMode="singleInstance" 它会影响您在应用程序中调用其他意图时的行为,例如使用 facebook sdk、paypal sdk 等..

      这里的解释很简单: link

      【讨论】:

      • FLAG_ACTIVITY_SINGLE_TOP 已被列为答案 - 同样感谢。
      【解决方案5】:

      我在这里有另一个解决方案:

          //Resume or restart the app (same as the launcher click)
          val resultIntent = Intent(context, MyLauncherActivity::class.java)
          resultIntent.addCategory(Intent.CATEGORY_LAUNCHER)
          resultIntent.setAction(Intent.ACTION_MAIN)
          resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
          val pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
          builder.setContentIntent(pendingIntent)  //builder is the notificationBuilder
      

      【讨论】:

        【解决方案6】:

        如果您在 .MainActivity 活动部分中将其从清单中删除:

        android:noHistory="true"
        

        这对于无法在 Android 7 之后的三星设备(和其他设备)上恢复应用程序是致命的

        【讨论】:

          猜你喜欢
          • 2021-01-04
          • 2011-12-30
          • 1970-01-01
          • 2013-11-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多