通知是android服务

时间:2018-07-21 10:52:48

标签: android android-service android-notifications

我有一条始于服务的通知。 当用户单击此通知时,活动开始。

我的问题是:如果用户在此活动中按“后退”按钮,则导航到主屏幕而不是上一个活动。

我想将新活动推送到现有任务。

    Intent openIntent = new Intent(this, MediaPlayerActivity.class);
    openIntent.setAction(ACTION.OPEN_ACTION);
    PendingIntent openPendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, "")
            .setContentTitle(bundle.getString("Title"))
            .setContentText(bundle.getString("Description"))
            .setContentIntent(openPendingIntent)
            .setShowWhen(false)
            .setOngoing(true)
            .build();

    startForeground(NOTIFICATION.PLAYER, notification);

示例1:

Activity A -> Activity B -> (Notification click)

我想要这个:

Activity A -> Activity B -> Activity Z

OR

示例2:

Activity A -> Activity B -> Activity C -> (Notification click)

我想要这个:

Activity A -> Activity B -> Activity C -> Activity Z

OR

示例3:

Activity A -> Activity B -> (Home button) -> (Notification click)

我想要这个:

Activity A -> Activity B -> Activity Z

但是我在所有示例中都拥有这个:

Activity Z ->(Back press)-> Home screen

2 个答案:

答案 0 :(得分:0)

您是否在该活动中覆盖了onBackPress方法?

为此,您最需要在该活动中定义onBackPress方法,如下所示,当用户按下后退按钮时转到MainActivity

@Override
public void onBackPressed() {
    finish();
    Intent intent=new Intent(thatActivity.this,MainActivity.class);
    startActivity(intent);
}

答案 1 :(得分:0)

最后我找到了解决方法here

通过添加以下行:

openIntent.addCategory(Intent.CATEGORY_LAUNCHER);