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

前端 未结 17 1368
借酒劲吻你
借酒劲吻你 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:51

    If you are running your code on 8.0 then application will crash. So start the service in the foreground. If below 8.0 use this :

    Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
    context.startService(serviceIntent);
    

    If above or 8.0 then use this :

    Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
    ContextCompat.startForegroundService(context, serviceIntent );
    

提交回复
热议问题