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

时间:2015-02-27 12:01:49

标签: android xamarin.android android-notifications android-pendingintent

我继承了这个(Xamarin)Android应用程序。它由几个活动组成,它还有一个响应GCM消息的BroadcastReceiver。收到GCM邮件时,BroadcastReceiver会在通知区域中发出通知(例如“某些商店或其他商店有促销活动”)。

通知附有PendingIntent,因此无论何时用户点击该通知,都会启动该应用的特定活动A.

显然,PendingIntent to start an activity需要一个Intent.FLAG_ACTIVITY_NEW_TASK标记,上面写着:

  

使用此标志时,如果任务已在您正在启动的活动中运行,则不会启动新活动;相反,当前任务将简单地以最后一个状态显示在屏幕的前面。

这确实是我所看到的行为。

  1. 当应用程序未处于活动状态时(我从未启动它或按下“返回”按钮足以结束任务)并单击通知,弹出活动A.的(好!)

  2. 当应用程序位于活动B的前台并且我点击通知时,没有任何事情发生(糟糕!我要求进行活动A)。

  3. 当我在活动B中打开应用程序时,我按下主页按钮,然后单击通知,应用程序返回前景,处于我按下Home之前的状态。 (糟糕!我要求参加活动A)。
  4. 问题:即使任务已在运行,我怎样才能打开活动A?

    这是我生成通知的方式:

    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);
    }
    

    谢谢!

1 个答案:

答案 0 :(得分:0)

PendingIntent你没有使用你要问的旗帜。这段代码:

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);
}

此外,您可能需要添加Intent.FLAG_ACTIVITY_REORDER_TO_FRONT。您还可以添加Intent.FLAG_ACTIVITY_CLEAR_TOP。像这样:

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);
}

您使用哪些标志取决于用户启动通知后“后退”按钮的工作方式。

文档说:

  

当用户通过按Home键离开任务时,当前活动将停止,其任务将进入后台。系统保留任务中每个活动的状态。如果用户稍后通过选择开始任务的启动器图标来恢复任务,则任务将到达前台并在堆栈顶部恢复活动。

来自:

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