记一次在生产环境时,安卓包在拍照后出现闪退的问题。
1、bug描述
【操作步骤】
1、app端进入租区报事,填写好表单信息;
2、添加图片-选择拍照-勾选已排好的照片
【实际结果】
出现白屏,然后应用闪退
【预期结果】
拍照上传成功
2、下面是错误日志
2022-12-19 17:56:56.465 26236-26236/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.crlandmixc.cpms, PID: 26236
java.lang.RuntimeException: Unable to create service com.luck.picture.lib.service.ForegroundService: java.lang.SecurityException: Permission Denial: startForeground from pid=26236, uid=10216 requires android.permission.FOREGROUND_SERVICE
at android.app.ActivityThread.handleCreateService(ActivityThread.java:5116)
at android.app.ActivityThread.access$3000(ActivityThread.java:278)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2565)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:9105)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:614)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1129)
Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=26236, uid=10216 requires android.permission.FOREGROUND_SERVICE
at android.os.Parcel.createExceptionOrNull(Parcel.java:2390)
at android.os.Parcel.createException(Parcel.java:2374)
at android.os.Parcel.readException(Parcel.java:2357)
at android.os.Parcel.readException(Parcel.java:2299)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7118)
at android.app.Service.startForeground(Service.java:715)
at com.luck.picture.lib.service.ForegroundService.onCreate(ForegroundService.java:41)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:5102)
at android.app.ActivityThread.access$3000(ActivityThread.java:278)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2565)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:9105)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:614)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1129)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:6841)
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:1522)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1166)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:16224)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3260)
callee: null 1508/2859
3、开发分析根因
4、参考文章
Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=12394, uid=10302 requires android.permission.FOREGROUND_SERVICE
翻译:原因:java.lang.SecurityException:权限拒绝:StartForeground from PID=12394,ULD=10302需要android.permission.foreground
Android在8.0开始限制后台服务,启动后台服务需要设置通知栏,使服务变成前台服务。同时在Android 9.0开始注册权限android.permission.FOREGROUND_SERVICE,并授权权限。
从8.0开始启动服务需要进行判断出来如下:
/**
* Android 8.0及以后,启动服务的形式已经变了
*/
private void startService(Context context) {
Intent intent = new Intent(context, MyForegroundService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
同时在Service的onCreate()方法里设置通知栏如下:
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("xxx", "xxx", NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager == null)
return;
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, "xxx")
.setAutoCancel(true)
.setCategory(Notification.CATEGORY_SERVICE)
.setOngoing(true)
.setPriority(NotificationManager.IMPORTANCE_LOW)
.build();
}
}