关闭活动而不显示它

nbysray5  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(290)

我有两个活动在我的代码,让a和b。从a开始,我使用意图启动b,当b启动时,我启动通知。
现在的问题是,当我关闭通知时,如果b对用户可见,它应该返回到a,否则当用户从最近的任务打开应用程序时,a应该出现。
我的想法是:在onresume中声明一个整数(可见),在onpause中声明0。所以我可以在oncreate中使用if来检查它。但是使用oncreate时,将调用onresume。如何克服这个问题?
编辑:我正在开发一款音乐播放器。a是文件选择器活动,b是播放器活动。我需要在b本身做通知,因为它是使用服务的前台通知(我需要播放器在后台运行)。因此,如果我在应用程序打开时关闭通知,播放器也必须关闭,否则下次我打开应用程序时,必须再次显示文件选择器。

avkwfej4

avkwfej41#

这是你想做的。a类:

Intent intent = new Intent(this,b.class);
startActivity(intent);

protected void onPause(){
   super.onPause();
   showNotification();
}
public void showNotification()
{
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Title")
            .setContentText("Description")
            .setAutoCancel(true);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = 1;
    createChannel(notificationManager);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

public void createChannel(NotificationManager notificationManager){
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationChannel channel = new NotificationChannel("channelID","title", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Description");
    notificationManager.createNotificationChannel(channel);
}

所以,如果通知关闭/从通知栏中删除,那么它将返回类a
然后,检查通知是否在类b中删除。

相关问题