【问题标题】:How to bring application to front when the notification icon is clicked (from Service)?单击通知图标时如何将应用程序置于前面(来自服务)?
【发布时间】:2011-07-04 10:08:27
【问题描述】:

我有一个在服务的帮助下播放媒体的应用程序。这在服务的帮助下运行顺利(创建了持续的通知)。当按下返回键时,应用程序进入后台。问题是当我单击通知图标时,每次都会创建我的应用程序的新实例。如何借助通知栏让我的应用从后台转到前台?

我的通知如下所示:

private void notifyMe() {
    final NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification note = new Notification(R.drawable.icon_audio, "Online!",
            System.currentTimeMillis());
    note.flags = Notification.FLAG_ONGOING_EVENT;
    PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, PlayMedia.class), 0);
    note.setLatestEventInfo(this, "Media Payer", "Online",i);
    mgr.notify(NOTIFY_ME_ID, note);
} 

解决方案 我通过在清单文件中添加以下行来让它工作:android:launchMode="singleInstance" 意图标志没有任何效果,认为这很奇怪......

【问题讨论】:

    标签: android


    【解决方案1】:

    您传递给 PendingIntent.getActivity 的意图需要设置适当的标志才能将正在运行的应用程序置于最前面。所以:

    Intent startActivity = new Intent(this,PlayMedia.class ); 
    startActivity.setFlags(Intent.*appropriate flag*); 
    PendingIntent i = PendingIntent.getActivity(this, 0, startActivity, 0);
    

    转到http://developer.android.com/reference/android/content/Intent.html 并搜索(ctrl+f)“flag_”。在那里,您将找到可以使用的标志列表。

    【讨论】:

    • 对于那些阅读这个问题+答案的人,虽然提问者接受这个答案是最好的,但这并不是解决他问题的解决方案。对于正确的解决方案,请阅读问题的最后部分
    • 如果他说你把这个放在 xml 中的哪个位置或显示 xml 代码会很好。
    • 据我记得,您将 launchMode 设置为清单中要更改其启动行为的活动元素的属性。
    • 尝试将 FLAG_ACTIVITY_SINGLE_TOP 作为适当的标志。为我工作:)
    【解决方案2】:

    只是答案的变体和对问题的评论。我不需要像 Alpar 建议的那样添加 android:launchMode="singleInstance" 。这就是我为使应用程序脱颖而出所做的工作。

    // this code brings the application from background to foreground  
    // when the Notification icon is clicked on.
    
    // create new notification
    int icon = R.drawable.ic_notify;
    Notification notification = new Notification(icon, "Ticker Text", System.currentTimeMillis());
    
    //  Create new Intent pointing to activity you want it to come back to
    Intent notificationIntent = new Intent(this, MainApp.class);
    
    // Add new intent to a pending intent
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    // add that pendingIntent to the new notification
    notification.setLatestEventInfo(getApplicationContext(), "Title", "Text", contentIntent);
    
    // send the intent to the NotificationManager
    ((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(UPLOADER_ID, notification);
    

    【讨论】:

      【解决方案3】:

      所有这些在 Android 5 中都不再起作用了。我尝试了上述所有建议,但每当我点击通知时,我的应用程序就会不断被破坏并重新启动。

      要修复它,我需要做的就是在 Intent 中添加 2 个标志:

      Intent resultIntent = new Intent(context, MainActivity.class);
      resultIntent.setAction(Intent.ACTION_MAIN);
      resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
      

      这将使您的应用重新回到前面,就好像它是从启动器屏幕点击一样。

      【讨论】:

      • 结合 android:launchMode="singleTask" 为我工作,所以 +1 因为所有其他人都没有
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多