如何在通知单击上恢复以前的活动操作

时间:2016-08-09 15:58:50

标签: android android-activity notifications android-notifications restore

我有一个正在执行某些下载功能的Activity。该活动将弹出一个进度对话框,并在下载时禁用用户交互以及创建通知进度。

我希望用户能够点击通知并将用户返回到具有显示进度和禁用用户交互的对话框的活动(基本上当用户进入主屏幕然后重新打开应用程序时会发生什么)。

我目前的尝试似乎重新启动了活动,下载仍在进行,但对话框已消失且用户交互已停用。我该如何解决这个问题?

我的方法:

设定意图行动

private static void buildNotification()
{
    //create intent  for onClick
    Intent resultIntent = new Intent(activity,  activity.GetType());
    resultIntent.SetAction(Intent.ActionMain);
    resultIntent.AddCategory(Intent.CategoryLauncher);
    PendingIntent resultPendingIntent = PendingIntent.GetActivity(activity, 0, resultIntent, PendingIntentFlags.UpdateCurrent);

    //set builder

    builder.SetContentIntent(resultPendingIntent);
    notificationManager = activity.GetSystemService(Context.NotificationService) as NotificationManager;
}

设置意图标志

private static void buildNotification()
{
    //create intent  for onClick
    Intent resultIntent = new Intent(activity,  activity.GetType());
    resultIntent.SetFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);
    PendingIntent resultPendingIntent = PendingIntent.GetActivity(activity, 0, resultIntent, PendingIntentFlags.UpdateCurrent);

    //set builder

    builder.SetContentIntent(resultPendingIntent);
    notificationManager = activity.GetSystemService(Context.NotificationService) as NotificationManager;
}

1 个答案:

答案 0 :(得分:1)

我最终只在意图上放了SingleTop标志。希望这有助于某人:

private static void buildNotification()
{
        //create intent  for onClick
    Intent resultIntent = new Intent(activity, activity.GetType());
    resultIntent.SetFlags(ActivityFlags.SingleTop);
    PendingIntent resultPendingIntent = PendingIntent.GetActivity(activity, 0, resultIntent, PendingIntentFlags.UpdateCurrent);

    //set builder

    builder.SetContentIntent(resultPendingIntent);
    notificationManager = activity.GetSystemService(Context.NotificationService) as NotificationManager;
}

顺便说一下,我正在使用Xamarin.Android