简体   繁体   English

Android:如何在任务已经运行时从通知启动活动?

[英]Android: how to launch activity from notification when task is already running?

I've inherited this (Xamarin) Android application. 我继承了这个(Xamarin)Android应用程序。 It consists of several activities and it also has a BroadcastReceiver that responds to GCM messages. 它由几个活动组成,它还有一个响应GCM消息的BroadcastReceiver Whenever a GCM message is received, the BroadcastReceiver puts a notification in the notification area (eg "There are promotions available at some store or another" ). 每当收到GCM消息时, BroadcastReceiver在通知区域中发出通知(例如“某些商店或其他商店有促销活动” )。

The notification has a PendingIntent attached to it, so that whenever a user clicks it, a certain activity A of the app is launched. 通知附加了PendingIntent ,因此无论何时用户点击它,都会启动应用程序的某个活动A.

Apparently, the PendingIntent to start an activity requires an Intent.FLAG_ACTIVITY_NEW_TASK flag, which says: 显然, 启动活动PendingIntent需要一个Intent.FLAG_ACTIVITY_NEW_TASK标志,该标志表示:

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; 使用此标志时,如果任务已在您正在启动的活动上运行,则不会启动新活动; instead, the current task will simply be brought to the front of the screen with the state it was last in. 相反,当前任务将简单地以最后一个状态被带到屏幕的前面。

This is indeed the behaviour I'm seeing. 这确实是我所看到的行为。

  1. When the app is not active (I never launched it or I pressed the Back button enough to end the task) and I click the notification, Activity A pops up. 当应用程序未处于活动状态时(我从未启动它或按下“返回”按钮足以结束任务),然后单击通知,弹出活动A. (good!) (好!)

  2. When the app is in the foreground in Activity B and I click the notification, nothing happens (bad! I asked for activity A). 当应用程序位于活动B的前台并且我点击通知时,没有任何反应(糟糕!我要求进行活动A)。

  3. When I have the app open in Activity B, I press the Home button and I click the notification, the app returns to the foreground in the state it was in before I pressed Home. 当我在活动B中打开应用程序时,我按下主页按钮,然后单击通知,应用程序返回前景,处于我按下Home之前的状态。 (bad! I asked for activity A). (糟糕!我要求参加活动A)。

Question : how can I make it so that activity A is opened even when a task is already running? 问题 :即使任务已在运行,我怎样才能打开活动A?

This is how I generate my notifications: 这是我生成通知的方式:

public void CreateNotification(Context context, string title, string desc)
{
    var notificationManager = GetSystemService(NotificationService) as NotificationManager;

    var notification = new Notification(Resource.Drawable.logosmallblue, title);
    notification.Flags = NotificationFlags.AutoCancel;
    notification.Sound = RingtoneManager.GetDefaultUri (RingtoneType.Notification);
    notification.Vibrate = new long[]{500,1500,500};

    var perPlaceUniqueId = desc.GetHashCode();

    // workaround for Kitkat bug (https://code.google.com/p/android/issues/detail?id=61850)
    GetNotificationPendingIntent(context, perPlaceUniqueId).Cancel();
    notification.SetLatestEventInfo(context, title, desc, GetNotificationPendingIntent(context, perPlaceUniqueId));

    notificationManager.Notify(perPlaceUniqueId, notification);
}

private PendingIntent GetNotificationPendingIntent(Context context, int requestCode)
{
    var uiIntent = new Intent(context, typeof(MainView));
    uiIntent.PutExtra(MainView.RECOMMENDED_TAG, true);

    return PendingIntent.GetActivity(context, requestCode, uiIntent, PendingIntentFlags.UpdateCurrent);
}

Thanks! 谢谢!

In your PendingIntent you did not use the flag you are asking about. 在您的PendingIntent您没有使用您要询问的旗帜。 This code: 这段代码:

private PendingIntent GetNotificationPendingIntent(Context context, int requestCode)
{
    var uiIntent = new Intent(context, typeof(MainView));
    uiIntent.PutExtra(MainView.RECOMMENDED_TAG, true);

    return PendingIntent.GetActivity(context, requestCode, uiIntent, PendingIntentFlags.UpdateCurrent);
}

Also, you probably need to add Intent.FLAG_ACTIVITY_REORDER_TO_FRONT . 此外,您可能需要添加Intent.FLAG_ACTIVITY_REORDER_TO_FRONT You could also add Intent.FLAG_ACTIVITY_CLEAR_TOP . 您还可以添加Intent.FLAG_ACTIVITY_CLEAR_TOP Something like this: 像这样的东西:

private PendingIntent GetNotificationPendingIntent(Context context, int requestCode)
{
    var uiIntent = new Intent(context, typeof(MainView));
    uiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    uiIntent.PutExtra(MainView.RECOMMENDED_TAG, true);

    return PendingIntent.GetActivity(context, requestCode, uiIntent, PendingIntentFlags.UpdateCurrent);
}

Which flags you use depends on how you want the "back" button to work after the user launches the notification. 您使用哪些标志取决于您在用户启动通知后希望“后退”按钮的工作方式。

The documentation says: 文件说:

When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. 当用户通过按Home键离开任务时,当前活动将停止,其任务将进入后台。 The system retains the state of every activity in the task. 系统保留任务中每个活动的状态。 If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack. 如果用户稍后通过选择开始任务的启动器图标来恢复任务,则任务将到达前台并在堆栈顶部恢复活动。

from this: 由此:

http://developer.android.com/guide/components/tasks-and-back-stack.html http://developer.android.com/guide/components/tasks-and-back-stack.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从已经通过appium打开的活动中启动Android应用 - How to launch Android app from already opened activity with appium Android如何在Activity已经运行时将数组从IntentService传递给Activity - Android how to pass array to activity from IntentService when Activity is already running Android:无法从通知启动活动 - Android : Unable to launch activity from notification 单击通知时从服务启动活动 - launch activity from service when notification is clicked 如何从推送通知启动后台运行 Android 应用程序? - how to launch background running android app from push notification? 如何在单击通知时启动活动? - How to launch an activity when notification is clicked? 如何从通知启动活动到前台? - How to launch an activity from a notification into the foreground? 在前台服务运行时从应用启动器和通知启动相同的活动 - Launch the same activity from both app launcher and notification when foreground service is running 调用onNewIntent()时如何检测我的Android Activity是否已在运行? - How to detect if my Android Activity was already running when onNewIntent() is called? Android:单击通知以启动活动 - Android : Click notification to launch activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM