Android 14适配

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

前台服务类型(Foreground service types are required)
前台服务类型(foregroundServiceType)是在 Android 10 引入的,通过 android:foregroundServiceType 可以指定 <service> 的服务类型,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 中的新类型。

<manifest ...>
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
    <application ...>
      <service
          android:name=".MyMediaPlaybackService"
          android:foregroundServiceType="mediaPlayback"
          android:exported="false">
      </service>
    </application>
</manifest>


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

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

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
 
<service
  android:name=".MyMediaPlaybackService"
  android:foregroundServiceType="mediaPlayback"
  android:exported="false">
</service>


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

注意:如果调用 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,例如:

<activity
    android:name=".AppActivity"
    android:exported="false">
    <intent-filter>
        <action android:name="com.example.action.APP_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>


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

// 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 中引入的这些接收器的功能,此要求有助于保护应用免受安全漏洞的影响。

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

// 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 标志。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值