最近刚刚做了Android 14的适配(即targetSdkVersion 升级到 34 ),通过此博客整理下相关注意点。
前台服务类型
当targetSdkVersion >= 34 ,应用内的前台服务(Foreground Service)需要指定至少一种前台服务类型。
在Android系统中,前台服务(Foreground Service)是一种特殊的服务,它可以在后台执行操作,同时向用户显示一个通知,这表示该服务正在执行任务。像音乐播放器、健身程序、定位程序,通过前台服务显示状态栏通知,让用户知道APP执行任务并且正在消耗系统资源。。Android10 引入了前台服务类型。(android:foregroundServiceType 属性)。
Android 8 (targetSdkVersion 28) 以后要用startForegroundService来启动"前台服务"。
private fun startLocationService() {
val intent = Intent(this, LocationService::class.java)
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}
Android 9及以后前台服务要在manifest中配置以下权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
前台应用启动服务后,要在service里面通过startForeground方法向通知栏发送一个通知:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = NotificationCompat.Builder(this, "channel_id").setContentTitle("前台服务").setContentText("前台服务正在运行中......").setSmallIcon(
R.drawable.ic_launcher_background).build()
startForeground(1, notification)
return super.onStartCommand(intent, flags, startId)
}
<service
android:name=".service.LocationService"
android:foregroundServiceType="camera"
android:enabled="true"
android:exported="true"></service>
常用的前台服务类型有:
camera
connectedDevice
dataSync
health
location
mediaPlayback
mediaProjection
microphone
phoneCall
remoteMessaging
shortService
specialUse
systemExempted
注意:如果您调用 startForeground() 但未声明适当的前台服务类型权限,系统会抛出 SecurityException。
安全相关
限制隐式Intent和PendingIntent
当targetSdk为34时,通过隐式Intent或隐式Intent创建的PendingIntent只能打开设置了android:exported="true"的组件,如果android:exported属性值为false,系统会抛出异常。(即应用必须使用明确的intent来打开(android:exported="false")的组件)通过上述限制可防止恶意应用拦截只供给用内部组件使用的隐式 intent。
如果应用创建一个PendingIntent ,但 intent 未指定组件或包,系统现在会抛出异常。
<activity
android:name=".NormalIntentMainActivity"
android:exported="false">
<intent-filter>
<action android:name="com.transsion.targetsdk34android.APP_ACTION"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
错误写法:(注意上面Activity的exported属性是false)
startActivity(Intent("com.transsion.targetsdk34android.APP_ACTION"))
报错信息如下:
E FATAL EXCEPTION: main
Process: com.transsion.targetsdk34android, PID: 6361
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.transsion.targetsdk34android.APP_ACTION }
正确写法:(注意上面Activity的exported属性是false)
val intent = Intent(MainActivity2@ this, NormalIntentMainActivity::class.java)
startActivity(intent)
运行时动态注册广播接收器必须指定导出行为
在 Android 14 上,运行时通过 Context的registerReceiver() 动态注册广播接收器,需要设置标记 RECEIVER_EXPORTED 或 RECEIVER_NOT_EXPORTED ,标识是否导出该广播,避免应用程序出现安全漏洞,如果注册的是系统广播,则不需要指定标记。(如果应用仅通过 Context的registerReceiver 方法为系统广播注册接收器时,那么它可以不在注册接收器时指定标志。)
选择广播接收器是否应导出并对设备上的其他应用可见。如果此接收器正在监听从系统或其他应用(甚至是您拥有的其他应用)发送的广播,请使用 RECEIVER_EXPORTED 标志。如果此接收器仅监听应用发送的广播,请使用 RECEIVER_NOT_EXPORTED 标志。
一些系统广播来自具有较高特权 这类应用(例如蓝牙和电话应用), 但不在系统的唯一进程 ID (UID) 下运行。接收者 接收所有系统广播,包括来自具有较高特权的广播 应用,请使用 RECEIVER_EXPORTED 标记您的接收器。
如果您使用 RECEIVER_NOT_EXPORTED 标记接收器, 接收器可以接收来自您的服务器 应用,但不从具有较高特权的应用进行广播。
如果您的应用要监听多个广播,但只应监听部分广播 已将RECEIVER_NOT_EXPORTED和部分标记为 RECEIVER_EXPORTED,将广播划分