Android TV 10应用工具开发问题:杀进程后无法重启目标应用
Android TV 10应用重启失败问题修复方案
核心问题分析
- 缓存清除逻辑错误:当前代码中的
this.cacheDir.deleteRecursively()仅清除自身应用缓存,并未处理目标应用的缓存。 - 无效启动代码干扰:代码末尾创建空
ComponentName的Intent并启动,会直接抛出异常,且finishAffinity()调用会打断目标应用的启动流程。 - TV应用启动适配缺失:目标TV应用的启动Intent需要包含
Intent.CATEGORY_LEANBACK_LAUNCHER分类,仅依赖getLaunchIntentForPackage可能在部分场景下失效。 - 系统权限限制:
CLEAR_APP_CACHE是受保护的系统权限,普通第三方应用无法直接获取,需采用兼容方案清除缓存。
修正步骤与代码实现
1. 修正MainActivity逻辑
移除无效代码,聚焦目标应用的重启与缓存清除,适配Android TV启动规则:
package com.deliadsolutions.cachekillerstarter import android.app.ActivityManager import android.content.Intent import android.net.Uri import android.os.Bundle import android.provider.Settings import androidx.fragment.app.FragmentActivity class MainActivity : FragmentActivity() { private val targetPackageName = "il.net.hot.hot" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 杀死目标应用进程 val am = getSystemService(ACTIVITY_SERVICE) as ActivityManager am.killBackgroundProcesses(targetPackageName) // 清除目标应用缓存(兼容方案) clearTargetAppCache() // 重启目标应用 restartTargetApp() // 关闭当前工具应用页面 finish() } private fun restartTargetApp() { val launchIntent = packageManager.getLaunchIntentForPackage(targetPackageName) if (launchIntent != null) { // 适配Android TV,添加LEANBACK启动分类 launchIntent.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER) // 确保启动新任务栈并清除原有栈 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(launchIntent) } else { // 目标应用未安装,跳转到应用商店 val intent = Intent(Intent.ACTION_VIEW).apply { data = Uri.parse("market://details?id=$targetPackageName") flags = Intent.FLAG_ACTIVITY_NEW_TASK } startActivity(intent) } } private fun clearTargetAppCache() { // 引导用户到应用详情页手动清除(无权限限制,兼容性最强) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { data = Uri.fromParts("package", targetPackageName, null) flags = Intent.FLAG_ACTIVITY_NEW_TASK } startActivity(intent) // 可选:系统签名/ROOT设备专用方案(不推荐普通应用使用) // try { // val method = packageManager.javaClass.getMethod("deleteApplicationCacheFiles", String::class.java, android.os.Handler::class.java) // method.invoke(packageManager, targetPackageName, null) // } catch (e: Exception) { // e.printStackTrace() // } } }
2. 调整AndroidManifest配置
移除重复权限,添加Android 11+所需的应用查询权限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <!-- Android 11及以上需要此权限查询其他应用信息 --> <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" /> <uses-feature android:name="android.hardware.touchscreen" android:required="false" /> <uses-feature android:name="android.software.leanback" android:required="true" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.Cachekillerstarter"> <activity android:name=".MainActivity" android:banner="@drawable/app_icon_your_company" android:exported="true" android:icon="@drawable/app_icon_your_company" android:label="@string/title_activity_main" android:logo="@drawable/app_icon_your_company" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DetailsActivity" android:exported="false" /> <activity android:name=".PlaybackActivity" android:exported="false" /> <activity android:name=".BrowseErrorActivity" android:exported="false" /> </application> </manifest>
关键注意事项
- 缓存清除限制:普通第三方应用无法直接操作其他应用的缓存,引导用户手动清除是唯一合法兼容的方案;若应用拥有系统签名或运行在ROOT设备上,可尝试注释中的隐藏API方案。
- 启动稳定性:添加
FLAG_ACTIVITY_CLEAR_TASK确保目标应用以全新状态启动,避免残留进程干扰。 - 调试建议:可添加日志或Toast提示启动失败情况,便于排查目标应用未安装或Intent获取失败的问题。
内容的提问来源于stack exchange,提问作者Dexter
相关产品推荐
相关产品推荐

