From 30de2439a8a271643c8ef0251b36f67fa6c71ed0 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Fri, 13 Jun 2025 21:48:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=80=9A=E7=9F=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../music_service/MusicService.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt b/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt index cd9f61791..26224f7f4 100644 --- a/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt +++ b/local_plugins/music_service/android/src/main/kotlin/com/yunqiinnovation/music_service/MusicService.kt @@ -30,6 +30,7 @@ class MusicService : MediaBrowserServiceCompat() { private const val TAG = "MusicService" private const val CHANNEL_ID = "music_media_service_channel" private const val NOTIFICATION_ID = 1002 + private const val DELETE_ACTION = "com.yunqiinnovation.music_service.DELETE_NOTIFICATION" private val isRunning = AtomicBoolean(false) private var currentUrlInternal: String? = null @@ -145,7 +146,22 @@ class MusicService : MediaBrowserServiceCompat() { if (currentStatus) PlaybackStateCompat.ACTION_PAUSE else PlaybackStateCompat.ACTION_PLAY ) val nextIntent = mediaButtonPendingIntent(PlaybackStateCompat.ACTION_SKIP_TO_NEXT) - + // 添加删除按钮Action + // 创建删除通知的PendingIntent + val deleteIntent = Intent(this, MusicService::class.java).apply { + action = DELETE_ACTION + } + val deletePendingIntent = PendingIntent.getService( + this, + 0, + deleteIntent, + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + } else { + PendingIntent.FLAG_UPDATE_CURRENT + } + ) + val builder = NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(title) .setContentText(artist) @@ -163,7 +179,10 @@ class MusicService : MediaBrowserServiceCompat() { playPauseIntent ) .addAction(android.R.drawable.ic_media_next, "下一首", nextIntent) + // 添加删除通知按钮 + .addAction(android.R.drawable.ic_menu_close_clear_cancel, "删除", deletePendingIntent) .setOngoing(currentStatus) + .setDeleteIntent(deletePendingIntent) // 设置通知被滑动删除时的行为 // 异步加载封面图 if (!coverUrl.isNullOrEmpty()) {