android 通过删除通知关闭应用程序

x4shl7ld  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(213)
@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(新安卓系统媒体应用通知兼容媒体样式()// .setMediaSession(媒体会话获取会话令牌()).setPriority(通知优先级_高).setC your text内容意图(内容意图).setOnlyAlertOnce(真).build();

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

}

这段代码在调用这个函数的任何地方创建通知,但是如果我想通过删除通知来关闭应用程序该怎么办

czq61nw1

czq61nw11#

使用setDeleteIntent方法-删除通知时,您可以向所有“上下文”对象发送一些广播(例如ActivityService
请注意,也有setOngoing方法,这将坚持你的通知和用户将不能删除它“手动”.然后你可以提供“杀死”行动按钮为这个通知,这可能表现完全一样为setDeleteIntent创建.在这种情况下不要忘记删除通知当用户刚刚离开/退出你的应用程序,当“杀死”行动按下或在任何其他“通常”的方式

相关问题