【问题标题】:Notification to bring Activity into foreground only if it is in background仅当 Activity 在后台时才将其带入前台的通知
【发布时间】:2014-12-16 15:07:49
【问题描述】:

我正在尝试制作一个类似于本机警报应用程序的应用程序。所以,这是我正在寻找的理想结果。 AlarmGoOffActivity 收到一个挂起的意图,它触发并显示带有贪睡和关闭按钮的警报,我已经完成了。它必须始终作为单个实例运行。并且实例应该在主页/后退按钮和通知图标的帮助下移动前景/背景(通知与警报同时发布)

但是,根据我编写的代码,我能够

1) 显示通知和警报。当点击通知时,无论它在哪里(前台或后台),都会被带到前台。

2) 当 Activity 进入前台时,会在实际警报屏幕之前显示一个白色屏幕以及操作栏。

这是我必须做的事情

1) 当用户检查警报并按下主屏幕或后退按钮并且活动进入后台时,我需要在通知的帮助下将其提前。 2) 但是,如果 Activity 已经在前台并且单击了通知图标,则不会发生任何事情。

如果您能建议我需要在此代码中更改哪些内容才能使其正常工作,那就太好了。

AndroidManifest.xml

   <activity
        android:name="com.alarm.productive.justalarm.Activities.AlarmGoOffActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">

    </activity>

AlarmGoOffActivity.java 通知部分

  NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.clock_logo)
                    .setContentTitle(utilFunctions.toCamelCase(currentAlarmInView.getName()))
                    .setContentText("Snooze or Dismiss Alarm");
    Intent resultIntent = new Intent(context, AlarmGoOffActivity.class);
resultIntent.putExtra(DBHelper.COLUMN_ID,currentAlarmInView.getId());
if(isSnooze){
    resultIntent.putExtra(DBHelper.TASK_TITLE,"snooze");

}

resultIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(AlarmGoOffActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    1001,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager =
            (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(currentAlarmInView.getId(), mBuilder.build());

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    您不需要使用TaskStackBuilder,因为您的任务仅包含 1 个活动。使用 TaskStackBuilder 会导致您的 Activity 重新启动,这不是您想要的。

    您不需要使用Intent.FLAG_ACTIVITY_REORDER_TO_FRONT。此标志用于重新排序任务中的活动。您的任务仅包含一个 Activity,因此将其重新排序到前面是没有用的,因为它已经在(任务的)前面。

    您需要使用Intent.FLAG_ACTIVITY_NEW_TASK,这将在新任务中启动 Activity(如果 Activity 尚未在现有任务中运行),或者只是将现有任务带到前台(如果它是已经运行)。

    改为这样创建您的通知:

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.clock_logo)
                    .setContentTitle(utilFunctions.toCamelCase(currentAlarmInView.getName()))
                    .setContentText("Snooze or Dismiss Alarm");
    Intent resultIntent = new Intent(context, AlarmGoOffActivity.class);
    resultIntent.putExtra(DBHelper.COLUMN_ID,currentAlarmInView.getId());
    if (isSnooze){
        resultIntent.putExtra(DBHelper.TASK_TITLE,"snooze");
    }
    // Setting this flag ensures that the task will be brought forward if
    //  it is in the background, but nothing will happen if it is already
    //  in the foreground
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(this,
                    1001,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    

    【讨论】:

    • 谢谢先生。这很有帮助。这正是我所需要的。
    • 非常感谢。我不仅解决了我的问题,而且还学到了很多新东西,再次感谢!
    • 好东西供快速参考。谢谢你。很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多