android引入androidx后出现的问题解决

最近在项目里面引入了androidx,在模拟器上运行正常,就发版本了,后来客户反映登录后程序崩溃,查看极光错误统计log发现问题如下:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2105)
	at android.os.Handler.dispatchMessage(Handler.java:109)
	at android.os.Looper.loop(Looper.java:166)
	at android.app.ActivityThread.main(ActivityThread.java:7383)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

应该是Notification问题只要在service中的onCreate()方法添加代码:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_STRING, "xxxx", NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(mChannel);
                Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
                startForeground(GRAY_SERVICE_ID, notification);
            }

 问题解决。

你可能感兴趣的:(Android)