You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

荣耀10 Lite自动启动权限页面跳转方法及应用适配崩溃问题求助

解决方案:荣耀设备自动启动权限引导适配与崩溃修复

一、崩溃原因分析

你的代码在荣耀设备上崩溃,核心原因有两个:

  1. 荣耀独立后,系统管理组件的包名和路径已经和华为不再完全一致,你当前使用的华为专属ComponentName在荣耀设备上大概率不存在,即使resolveActivity返回非null,实际执行时也会触发异常;
  2. 代码里存在变量引用错误:你定义了powerManagerIntent数组,但循环时却引用了Utils.powerManagerIntent,如果Utils类中没有这个变量,会直接引发空指针崩溃。

二、崩溃修复与基础功能补全

先修正代码里的错误,同时添加异常捕获逻辑,避免因系统组件不存在导致崩溃,还补全之前缺失的启动设置页面逻辑:

val powerManagerIntents = arrayOf(
    // 华为原有自动启动相关页面
    Intent().setComponent(
        ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity" )
    ),
    Intent().setComponent(
        ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity" )
    ),
    Intent().setComponent(
        ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity" )
    )
)

for (intent in powerManagerIntents) {
    try {
        if (packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            MaterialAlertDialogBuilder(this@StartChildModeActivity, R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
                .setTitle("Enable AutoStart")
                .setMessage("Please allow AppName to always run in the background, else our services can't be accessed.")
                .setCancelable(false)
                .setNegativeButton(resources.getString(R.string.btn_cancel)) { dialog, _ -> dialog.dismiss() }
                .setPositiveButton("ALLOW") { _, _ ->
                    // 补全启动设置页面的逻辑
                    startActivity(intent)
                }
                .show()
            // 找到可用的Intent后立即跳出循环,避免弹出多个对话框
            break
        }
    } catch (e: Exception) {
        // 捕获所有可能的异常(组件不存在、权限不足等),防止崩溃
        e.printStackTrace()
    }
}

三、荣耀10 Lite自动启动权限页面适配

荣耀10 Lite使用Magic UI,自动启动权限的系统组件包名已改为com.hihonor.systemmanager,你需要把荣耀专属的Intent添加到数组最前面,优先匹配:

val powerManagerIntents = arrayOf(
    // 荣耀专属自动启动页面(优先匹配)
    Intent().setComponent(
        ComponentName("com.hihonor.systemmanager", "com.hihonor.systemmanager.startupmgr.ui.StartupNormalAppListActivity")
    ),
    Intent().setComponent(
        ComponentName("com.hihonor.systemmanager", "com.hihonor.systemmanager.appcontrol.activity.StartupAppControlActivity")
    ),
    // 华为原有组件
    Intent().setComponent(
        ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity" )
    ),
    Intent().setComponent(
        ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity" )
    ),
    Intent().setComponent(
        ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity" )
    )
)

四、额外优化建议

  • 品牌精准匹配:可以先判断设备品牌,只对华为/荣耀设备显示引导,避免其他品牌设备做无效循环:
    val brand = Build.BRAND.lowercase()
    if (brand.contains("huawei") || brand.contains("honor")) {
        // 执行上述Intent循环逻辑
    }
    
  • 兜底方案:如果所有专属Intent都无法匹配,添加应用详情页跳转作为兜底,让用户手动查找权限:
    val fallbackIntent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
        data = Uri.fromParts("package", packageName, null)
    }
    // 将这个Intent添加到数组最后
    

内容的提问来源于stack exchange,提问作者The Bala

火山引擎 最新活动