You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现移除Android通知时关闭应用?

实现移除通知时关闭应用的方案

原始代码(修正语法错误后)

@RequiresApi(api = Build.VERSION_CODES.M)
public void showNotification(int playPauseBtn) {

    Intent intent = new Intent(this, ExoActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);

    Intent prevIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PREVIOUS);
    PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 0, prevIntent,
            PendingIntent.FLAG_MUTABLE);

    Intent playIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PLAY);
    PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 0, playIntent,
            PendingIntent.FLAG_MUTABLE);

    Intent nextIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_NEXT);
    PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 0, nextIntent,
            PendingIntent.FLAG_MUTABLE);

    Bitmap picture = BitmapFactory.decodeResource(getResources(), HelperClass.getlist().get(position).getImg());
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
            .setSmallIcon(HelperClass.getlist().get(position).getImg())    //get thumbnail will have small icon pic
            .setLargeIcon(picture)
            .setContentTitle(HelperClass.getlist().get(position).getName())
            .addAction(R.drawable.ic_baseline_skip_previous_24, "Previous", prevPendingIntent)
            .addAction(R.drawable.ic_baseline_play_arrow_24, "Play", playPendingIntent)
            .addAction(R.drawable.ic_baseline_skip_next_24, "Next", nextPendingIntent)
          //.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
          //.setMediaSession(mediaSession.getSessionToken()))
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(contentIntent)
            .setOnlyAlertOnce(true).build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);

}

实现步骤

1. 创建通知删除广播接收器

新建一个广播接收器类,处理通知被移除的事件:

public class NotificationDeleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 彻底关闭应用所有任务栈
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager != null) {
            for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
                task.finishAndRemoveTask();
            }
        }
    }
}

2. 在Manifest中注册接收器

在AndroidManifest.xml里添加接收器注册:

<receiver android:name=".NotificationDeleteReceiver" />

3. 修改通知构建代码,添加删除触发逻辑

在原有的通知构建链中,添加setDeleteIntent指定删除通知时的PendingIntent:

// 创建通知删除触发的Intent
Intent deleteIntent = new Intent(this, NotificationDeleteReceiver.class);
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this, 1, deleteIntent, PendingIntent.FLAG_MUTABLE);

// 构建通知时加入删除Intent
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
        // 保留原有所有配置项...
        .setContentIntent(contentIntent)
        .setDeleteIntent(deletePendingIntent) // 新增这一行
        .setOnlyAlertOnce(true).build();

关键说明

  • setDeleteIntent会在用户手动划掉通知、系统自动移除通知时触发广播。
  • 使用finishAndRemoveTask()能彻底清除应用的任务栈,确保完全退出;如果仅需关闭当前Activity,可根据场景调整逻辑。
  • PendingIntent的请求码(示例中用1)要和其他PendingIntent区分开,避免逻辑冲突。

内容的提问来源于stack exchange,提问作者Khushi Saxena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 12:27:51