新旧版兼容问题

Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent

错误原因:
Android 8.0 不再允许后台service直接通过startService方式去启动。

解决办法:

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());
}

引用 https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten

你可能感兴趣的:(新旧版兼容问题)