Android 14适配

06-01 1004阅读

Google I/O 2023 发布的 Android beta2 ,Android 14 将在2023年第三季度发布。Google Play 已经开始强制要求targetSdkVersion 33适配,所以 targetSdkVersion 34适配也是非常有必要的。

Android 14适配

前台服务类型(Foreground service types are required)

前台服务类型(foregroundServiceType)是在 Android 10 引入的,通过 android:foregroundServiceType 可以指定 的服务类型,targetSdkVersion 34 的情况下,必须为应用内的每个前台服务(foreground-services) 指定至少一种前台服务类型。此列表显示可供选择的前台服务类型:

  • camera
  • connectedDevice
  • dataSync
  • health
  • location
  • mediaPlayback
  • mediaProjection
  • microphone
  • phoneCall
  • remoteMessaging
  • shortService
  • specialUse
  • systemExempted

    如果你 App 中的用例与这些类型中的任何一种都不相关,那么建议还是将服务迁移成 WorkManager 或 jobs 。WorkManager or user-initiated data transfer jobs.

    health、remoteMessaging、shortService、specialUse 和 systemExempted 类型是 Android 14 中的新类型。

      
      
        
          
          
        
    

     如果面向 Android 14 的应用未在清单中定义给定服务的类型,则系统将在为该服务调用 startForeground() 时引发 MissingForegroundServiceTypeException。

    声明使用前台服务类型的新权限

    如果以 Android 14 为目标平台的应用使用前台服务,则它们必须根据 Android 14 引入的前台服务类型声明特定权限。 

    
    

    所有权限都定义为普通权限,并默认授予。 用户无法撤销这些权限。

    注意:如果调用 startForeground() 时未声明适当的前台服务类型权限,系统将抛出 SecurityException。

    在运行时包括前台服务类型

    启动前台服务的应用程序的最佳做法是使用 startForeground()。在其中传入前台服务类型的按位整数,可以选择传递一个或多个类型值。

    如果启动使用以下任何类型的前台服务,则每次为该服务调用 startForeground() 时都应始终包含这些类型:

    • FOREGROUND_SERVICE_TYPE_CAMERA
    • FOREGROUND_SERVICE_TYPE_LOCATION
    • FOREGROUND_SERVICE_TYPE_MICROPHONE
      Service.startForeground(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION)

      对implicit(隐式)和pending(未决) intents(意图)的限制

      对于面向 Android 14 的应用,Android 通过以下方式限制应用向内部应用组件发送隐式意图:

      • 隐式 intent 仅传递给导出的组件,应用必须使用明确的 intent 来交付给未导出的组件,或者将组件标记为已导出(exported) 。
      • 如果应用创建一个 mutable pending intent ,但 intent 未指定组件或包,系统现在会抛出异常。

        这些更改可防止恶意应用拦截只供给用内部组件使用的隐式 intent,例如:

            
                
                
            
        

        如果您的应用尝试使用隐式意图启动此活动,则会抛出异常:

        // Throws an exception when targeting Android 14.
        context.startActivity(Intent("com.example.action.APP_ACTION"))

        要启动未导出的 Activity,您的应用应改用显式 Intent:

        // This makes the intent explicit.
        val explicitIntent =
                Intent("com.example.action.APP_ACTION")
        explicitIntent.apply {
            package = context.packageName
        }
        context.startActivity(explicitIntent)

        运行时注册的广播接收器必须指定导出行为

        以 Android 14 为目标并使用 context-registered receivers (ContextCompat.registerReceiver)的用和服务的需要指定一个标志,以指示接收器是否应导出到设备上的所有其他应用:分别为 RECEIVER_EXPORTED 或 RECEIVER_NOT_EXPORTED。通过利用 Android 13 中引入的这些接收器的功能,此要求有助于保护应用免受安全漏洞的影响。

        1. 启用 DYNAMIC_RECEIVER_EXPLICIT_EXPORT_REQUIRED 兼容性框架更改。
        2. 在应用的每个广播接收器中,明确指明其他应用是否可以向其发送广播,如以下代码段所示:

        // This broadcast receiver should be able to receive broadcasts from other apps.
        // This option causes the same behavior as setting the broadcast receiver's
        // "exported" attribute to true in your app's manifest.
        context.registerReceiver(sharedBroadcastReceiver, intentFilter,
            RECEIVER_EXPORTED)
        // For app safety reasons, this private broadcast receiver should **NOT**
        // be able to receive broadcasts from other apps.
        context.registerReceiver(privateBroadcastReceiver, intentFilter,
            RECEIVER_NOT_EXPORTED)

        仅接收系统广播的接收器例外

        如果您的应用仅通过 Context#registerReceiver 方法(例如 Context#registerReceiver())为系统广播注册接收器,则在注册接收器时不应指定标志。

        从后台启动Activity的限制

        针对 Android 14 的应用,系统限制了应用在后台启动 Activity :

        • 当应用使用 PendingIntent#send()发送  PendingIntent 以及类似行为时,如果应用想要授予其自己的后台 service 启动权限以启动 pending intent,则该应用现在必须选择加入一个 ActivityOptions ,具体为带有 setPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
        • 当一个可见应用使用 bindService() 绑定另一个在后台运行的应用的服务时,如果该可见应用想要将其自己的后台 activity 启动权限授予绑定服务,则它现在必须选择加入 BIND_ALLOW_ACTIVITY_STARTS 标志。
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码