
我有一个运行良好的项目,但把compileSdkVersion从28修改到29之后,在Android Q(10.0)+的手机上相机预览时失败了,并且我很确定已经在清单文件声明了权限,也在运行时申请了相机权限:
<uses-permission android:name="android.permission.CAMERA" />
我的相机预览是在一个后台Service上运行的,预览出错时,相关打印如下:
Access denied finding property "vendor.camera.aux.packagelist"
...
An error occurred while connecting to camera 0: Status(-8, EX_SERVICE_SPECIFIC): '6: connectHelper:1915: Camera "0" disabled by policy'
查看谷歌最新的开发文档后,发现还要为service增加foregroundServiceType,谷歌的骚操作真是越来越多了~~
<service android:name=".core.SipService"
android:exported="true"
android:enabled="true"
android:foregroundServiceType="camera|microphone|location">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.vanrui.sip"/>
</intent-filter>
</service>
或者在代码运行时执行:
Notification notification = ...;
Service.startForeground(notification, FOREGROUND_SERVICE_TYPE_LOCATION | FOREGROUND_SERVICE_TYPE_CAMERA | FOREGROUND_SERVICE_TYPE_MICROPHONE);
现在,一切运行正常了。

这是一个风趣幽默的博主,不定时分享技术和生活干货,赶紧关注一波吧~~