如何通过单击通知来关闭我的应用程序的任何活动?

2023-12-14

当我单击通知时,应用以下操作:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

在应用程序的所有“startActivity”中,我应用了下一个标志:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

我的通知的 startActivity 执行以下操作: 将活动称为“Splash”并称为“Main”。

Casulidad 如果我处于“主”脉冲通知中,则关闭电流(工作正常)。 但是,如果我在“新闻”活动中并发出通知,我会打开 2 个活动,即新的“主要”活动和以前的“新闻”活动。

如何通过单击通知来关闭我的应用程序的任何活动?


您可以在通知中设置一个 PendingIntent,该通知将被广播接收器中的 Activity 捕获。然后在activity的broadcastReceiver中调用finish();这应该会关闭您的活动。

IE。 将其放入您的活动中

BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        finish();
    }

};

这在你的onCreate()

IntentFilter filter = new IntentFilter("android.intent.CLOSE_ACTIVITY");
registerReceiver(mReceiver, filter);

然后你的通知中的 PendingIntent 应该有以下操作"android.intent.CLOSE_ACTIVITY"为了安全起见,请提供您的活动套餐。

这是由

Intent intent = new Intent("android.intent.CLOSE_ACTIVITY");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0 , intent, 0);

然后使用以下命令将其添加到您的通知中setContentIntent(pIntent)使用Notification.Builder构建通知时。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过单击通知来关闭我的应用程序的任何活动? 的相关文章

随机推荐