在应用程序启动时,应用程序启动应该执行一些网络任务的服务。 在目标API级别26后,我的应用程序无法在Android 8.0后台启动服务。

导致原因:java.lang.IllegalStateException:不允许启动 服务意图{ cmp = my.app.tt / com.my.service }: app是在后台uid UidRecord{90372b1 u0a136 CEM空闲procs:1 seq (0, 0, 0)}

我的理解是: 后台执行限制

startService()方法现在抛出一个IllegalStateException 针对Android 8.0的应用程序尝试使用这种方法 不允许创建后台服务。

“在不被允许的情况下”——这实际上是什么意思?以及如何修复它。我不想把我的服务设置为前台


当前回答

它实际上发生了,因为手机在屏幕外,或者你在启动服务时按了电源键。解决这个问题的方法对我来说是 启动一个活动,当它将进入onResume然后启动服务。 在我的例子中,它是启动和启动服务。

其他回答

我也有这个问题

添加了这个库

implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

重新安装了应用程序帮我解决了这个问题

使用startForegroundService()代替startService() 并且不要忘记创建startForeground(1,new Notification());在您的服务5秒内开始服务。

如果任何意图之前工作正常时,应用程序是在后台,这将不再是Android 8及以上的情况下。只引用意图,当app在后台时,它必须做一些处理。

必须遵循以下步骤:

Above mentioned intent should be using JobIntentService instead of IntentService. The class which extends JobIntentService should implement the - onHandleWork(@NonNull Intent intent) method and should have below the method, which will invoke the onHandleWork method: public static void enqueueWork(Context context, Intent work) { enqueueWork(context, xyz.class, 123, work); } Call enqueueWork(Context, intent) from the class where your intent is defined. Sample code: Public class A { ... ... Intent intent = new Intent(Context, B.class); //startService(intent); B.enqueueWork(Context, intent); }

下面的类以前扩展了Service类

Public Class B extends JobIntentService{
...

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, B.class, JobId, work);
    }

    protected void onHandleWork(@NonNull Intent intent) {
        ...
        ...
    }
}

com.android。支持:支持compat需要JobIntentService -我使用26.1.0 V。 最重要的是确保Firebase库的版本至少是10.2.1,我在10.2.0有问题——如果你有的话! 你的manifest应该具有Service类的以下权限: 服务android: name = "。B” android:出口= " false " android:许可= " android.permission.BIND_JOB_SERVICE "

希望这能有所帮助。

最好的方法是使用JobIntentService,它为Oreo使用新的JobScheduler,如果不可用则使用旧的服务。

在舱单上申报:

<service android:name=".YourService"
         android:permission="android.permission.BIND_JOB_SERVICE"/>

在你的服务中,你必须用onHandleWork替换onHandleIntent:

public class YourService extends JobIntentService {

    public static final int JOB_ID = 1;

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, YourService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        // your code
    }

}

然后你开始你的服务:

YourService.enqueueWork(context, new Intent());

它实际上发生了,因为手机在屏幕外,或者你在启动服务时按了电源键。解决这个问题的方法对我来说是 启动一个活动,当它将进入onResume然后启动服务。 在我的例子中,它是启动和启动服务。