[英]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. 这确实是我所看到的行为。
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!)
(好!)
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)。
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.