You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

索尼Android 11+设备图标角标崩溃:Unknown URL异常排查求助

异常原因分析与解决方案

崩溃信息

所有崩溃设备系统版本为Android 11、12、13及以上

Fatal Exception: java.lang.IllegalArgumentException: Unknown URL content://com.sonymobile.home.resourceprovider/badge
   at android.content.ContentResolver.insert(ContentResolver.java:2192)
   at android.content.ContentResolver.insert(ContentResolver.java:2157)
   at android.content.AsyncQueryHandler$WorkerHandler.handleMessage(AsyncQueryHandler.java:96)
   at qd.s$a.handleMessage(SonyAsyncQueryHandler.java:1)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loopOnce(Looper.java:346)
   at android.os.Looper.loop(Looper.java:475)
   at android.os.HandlerThread.run(HandlerThread.java:67)

当前设置角标的代码

val contentValues = ContentValues()
contentValues.put("badge_count", count)
contentValues.put("package_name", context.packageName)
contentValues.put("activity_name", launcherClassName)
val asyncQueryHandler = SonyAsyncQueryHandler(context.contentResolver)
asyncQueryHandler.startInsert(0, null, Uri.parse("content://com.sonymobile.home.resourceprovider/badge"), contentValues)

异常原因

  • 目标ContentProvider不可用:content://com.sonymobile.home.resourceprovider/badge是索尼桌面专属的ContentProvider Uri,部分索尼设备可能未预装该组件、用户手动禁用,或是高版本Android系统中该Uri路径被修改/移除。
  • Android 11+软件包可见性限制:系统默认限制应用访问未声明的其他应用组件,若未声明对索尼桌面应用的可见性,会直接阻断对其ContentProvider的访问,触发Uri不存在异常。
  • 非索尼设备执行逻辑:代码硬编码索尼专属Uri,若运行在其他品牌设备上,因不存在对应ContentProvider必然崩溃。

可行解决方案

  1. 前置校验设备与Provider可用性
    执行角标设置前,先验证设备品牌及目标Uri的可访问性,避免无效执行:

    fun isSonyBadgeProviderAvailable(context: Context): Boolean {
        return try {
            val uri = Uri.parse("content://com.sonymobile.home.resourceprovider/badge")
            context.contentResolver.call(uri, "check", null, null) != null
        } catch (e: Exception) {
            false
        }
    }
    
    // 使用示例
    if (Build.BRAND.equals("sony", ignoreCase = true) && isSonyBadgeProviderAvailable(context)) {
        // 执行索尼角标设置逻辑
    }
    
  2. 添加软件包可见性声明
    在AndroidManifest.xml中添加对索尼桌面应用的可见性声明,适配Android 11+权限规则:

    <queries>
        <package android:name="com.sonymobile.home" />
    </queries>
    
  3. 多品牌设备兼容方案
    不要仅依赖索尼私有Uri,针对不同品牌设备实现对应角标逻辑:

    • 三星:使用com.sec.android.provider.badge Uri
    • 华为:调用官方提供的BadgeHelper工具类
    • Android 13+:使用系统统一API NotificationManager.setBadgeNotificationCount()
  4. 异常捕获兜底
    即使做了前置校验,仍可能出现异常,用try-catch包裹逻辑避免崩溃:

    try {
        val contentValues = ContentValues()
        contentValues.put("badge_count", count)
        contentValues.put("package_name", context.packageName)
        contentValues.put("activity_name", launcherClassName)
        val asyncQueryHandler = SonyAsyncQueryHandler(context.contentResolver)
        asyncQueryHandler.startInsert(0, null, Uri.parse("content://com.sonymobile.home.resourceprovider/badge"), contentValues)
    } catch (e: IllegalArgumentException) {
        // 处理Uri不存在异常,可做降级提示或跳过
    } catch (e: Exception) {
        // 其他异常兜底
    }
    

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 08:12:43