From 4705ec0c04034c8190451976092732df7c9b1d08 Mon Sep 17 00:00:00 2001 From: Autfy <49226960+Autfy@users.noreply.github.com> Date: Sun, 8 Feb 2026 12:19:58 +0800 Subject: [PATCH] Handle missing app launch intent and update pending intent flags --- .../andro_accessibility_api/demo/actions/actoins.kt | 9 +++++++-- .../demo/service/ForegroundService.kt | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/cn/vove7/andro_accessibility_api/demo/actions/actoins.kt b/app/src/main/java/cn/vove7/andro_accessibility_api/demo/actions/actoins.kt index c526d1c..747b3bc 100644 --- a/app/src/main/java/cn/vove7/andro_accessibility_api/demo/actions/actoins.kt +++ b/app/src/main/java/cn/vove7/andro_accessibility_api/demo/actions/actoins.kt @@ -143,7 +143,12 @@ class WaitAppAction : Action { delay(1000) val targetApp = "com.android.chrome" - act.startActivity(act.packageManager.getLaunchIntentForPackage(targetApp)) + val launchIntent = act.packageManager.getLaunchIntentForPackage(targetApp) + if (launchIntent == null) { + toast("未找到应用: $targetApp") + return + } + act.startActivity(launchIntent) if ( waitForApp(targetApp, 5000).also { @@ -271,4 +276,4 @@ class TraverseAllAction : Action { // assert = [ Bottom, SubView ] } -} \ No newline at end of file +} diff --git a/app/src/main/java/cn/vove7/andro_accessibility_api/demo/service/ForegroundService.kt b/app/src/main/java/cn/vove7/andro_accessibility_api/demo/service/ForegroundService.kt index 4569326..e105c9a 100644 --- a/app/src/main/java/cn/vove7/andro_accessibility_api/demo/service/ForegroundService.kt +++ b/app/src/main/java/cn/vove7/andro_accessibility_api/demo/service/ForegroundService.kt @@ -46,7 +46,12 @@ class ForegroundService : Service() { setContentText("输出布局 on logcat") val printIntent = Intent(this@ForegroundService, ForegroundService::class.java) printIntent.action = ACTION_PRINT_LAYOUT - val pi = PendingIntent.getService(this@ForegroundService, 0, printIntent, 0) + val pendingFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + } else { + PendingIntent.FLAG_UPDATE_CURRENT + } + val pi = PendingIntent.getService(this@ForegroundService, 0, printIntent, pendingFlags) setVisibility(NotificationCompat.VISIBILITY_PUBLIC) setSmallIcon(R.mipmap.ic_launcher_round) @@ -86,4 +91,4 @@ class ForegroundService : Service() { companion object { const val ACTION_PRINT_LAYOUT = "print_layout" } -} \ No newline at end of file +}