Android8.0后台服务限制适配

Android8.0的 后台服务限制 导致如下报错:

--------- beginning of crash
E/AndroidRuntime( 4237): FATAL EXCEPTION: main
E/AndroidRuntime( 4237): Process: com.ansen.appsettings, PID: 4237
E/AndroidRuntime( 4237): java.lang.RuntimeException: Unable to start receiver com.ansen.appsettings.receiver.OnBootCheck: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.ansen.appsettings/.services.EnableProfileService }: app is in background uid null
E/AndroidRuntime( 4237): 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:3197)
E/AndroidRuntime( 4237): 	at android.app.ActivityThread.-wrap17(Unknown Source:0)
E/AndroidRuntime( 4237): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675)
E/AndroidRuntime( 4237): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime( 4237): 	at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime( 4237): 	at android.app.ActivityThread.main(ActivityThread.java:6518)
E/AndroidRuntime( 4237): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 4237): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E/AndroidRuntime( 4237): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
E/AndroidRuntime( 4237): Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.ansen.appsettings/.services.EnableProfileService }: app is in background uid null
E/AndroidRuntime( 4237): 	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1535)
E/AndroidRuntime( 4237): 	at android.app.ContextImpl.startServiceAsUser(ContextImpl.java:1508)
E/AndroidRuntime( 4237): 	at android.content.ContextWrapper.startServiceAsUser(ContextWrapper.java:666)
E/AndroidRuntime( 4237): 	at android.content.ContextWrapper.startServiceAsUser(ContextWrapper.java:666)
E/AndroidRuntime( 4237): 	at com.ansen.appsettings.receiver.OnBootCheck.onReceive(OnBootCheck.java:33)
E/AndroidRuntime( 4237): 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:3190)
E/AndroidRuntime( 4237): 	... 8 more
I/ActivityManager( 1142): Showing crash dialog for package com.ansen.appsettings u0

解决:

  • 1.系统不允许后台应用创建后台服务.因此,Android 8.0 引入了一种全新的方法,即 Context.startForegroundService(),以在前台启动新服务.

通俗的说 将原来 startService方式启动服务修改为 startForegroundService启动服务

        Intent checkIntent = new Intent();
        checkIntent.setComponent(new ComponentName(context, UpdateCheckService.class));
        checkIntent.setAction(UpdateCheckService.ACTION_MANUAL_CHECK);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            context.startForegroundService(checkIntent);
        } else {
            context.startService(checkIntent);
        }
  • 2.在系统创建服务后,应用有五秒的时间来调用该服务的startForeground()方法以显示新服务的用户可见通知(如果应用在此时间限制内未调用 startForeground(),则系统将停止服务并声明此应用为 ANR)

通俗的说 在服务的onCreate()方法中调用startForeground()即可

    private static final String channelID = "UpdateCheck_channel_id";
    private static final String channelName = "UpdateCheck_channelname";

    @Override
    public void onCreate() {
        super.onCreate();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            createNotificationChannel();
            Notification notification = new Notification.Builder(getApplicationContext(), channelID).build();
            startForeground(1, notification);
        }
    }
    
    private void createNotificationChannel(){
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(notificationChannel);
    }

简单调用startForeground()即可,至于上述代码为什么这么多?

因为Android 8.0 引入了通知渠道,所以Notification 需要添加 channelId 才可以正常使用,不然会有如下报错:

Android 8.0 功能和 API

E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.ansen.autoupdater, PID: 3934
E AndroidRuntime: 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)
E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6518)
E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

你可能感兴趣的:(Android,#,Basic)