我正在尝试通过以下代码调用MIUI系统活动:
val intent = Intent()
intent.setClassName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsConfigActivity")
intent.putExtra("package_name", activity.packageName)
intent.putExtra("package_label", getText(R.string.app_name))
startActivity(intent)
在 Android 13 之前,一切正常,但是 在 Android 13 上 我遇到错误:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.miui.powerkeeper/com.miui.powerkeeper.ui.HiddenAppsConfigActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared <intent-filter>?
尝试添加默认意图过滤器,但没有帮助:
<activity android:name="com.miui.powerkeeper.ui.HiddenAppsConfigActivity"
android:exported="true">
<intent-filter>
<action android:name="foo" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
如何定义外部意图过滤器?
问题是我确定了 MIUI 固件的存在,如下所示:
fun isMIUI() = !TextUtils.isEmpty(AndroidUtilities.getSystemProperty("ro.miui.ui.version.name"))
但该错误在宏伟的Redmi A2+上重现,其固件为Android 13(Go)。
这些固件没有安装专有的 MIUI 应用程序,这就是我无法运行它们的原因。
因此,要解决该问题,需要将固件标识更改为:
fun isMIUI(context: Context): Boolean {
val intentList = listOf(
Intent("miui.intent.action.OP_AUTO_START").addCategory(Intent.CATEGORY_DEFAULT),
Intent().setComponent(ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")),
Intent("miui.intent.action.POWER_HIDE_MODE_APP_LIST").addCategory(Intent.CATEGORY_DEFAULT),
Intent().setComponent(ComponentName("com.miui.securitycenter", "com.miui.powercenter.PowerSettings"))
)
intentList.forEach { intent ->
val resolved = Internal.resolveActivityList(context, intent).isNotEmpty()
if (resolved) return true
}
return false
}
解决方法:
@Suppress("DEPRECATION")
fun resolveActivityList(context: Context, intent: Intent): List<ResolveInfo> = with(context.packageManager) {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
} else queryIntentActivities(intent, PackageManager.GET_META_DATA)
}