Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent

前端 未结 17 1271
借酒劲吻你
借酒劲吻你 2020-11-22 09:15

On application launch, app starts the service that should to do some network task. After targeting API level 26, my application fails to start service on Android 8.0 on back

17条回答
  •  眼角桃花
    2020-11-22 09:26

    I got solution. For pre-8.0 devices, you have to just use startService(), but for post-7.0 devices, you have to use startForgroundService(). Here is sample for code to start service.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, ServedService.class));
        } else {
            context.startService(new Intent(context, ServedService.class));
        }
    

    And in service class, please add the code below for notification:

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(1,new Notification());
    }
    

    Where O is Android version 26.

    If you don't want your service to run in Foreground and want it to run in background instead, post Android O you must bind the service to a connection like below:

    Intent serviceIntent = new Intent(context, ServedService.class);
    context.startService(serviceIntent);
    context.bindService(serviceIntent, new ServiceConnection() {
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
             //retrieve an instance of the service here from the IBinder returned 
             //from the onBind method to communicate with 
         }
    
         @Override
         public void onServiceDisconnected(ComponentName name) {
         }
    }, Context.BIND_AUTO_CREATE);
    

提交回复
热议问题