在应用程序启动时,应用程序启动应该执行一些网络任务的服务。 在目标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的应用程序尝试使用这种方法 不允许创建后台服务。

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


当前回答

我看到很多回复都建议只使用前台服务。为了使用ForegroundService,必须有一个与之关联的通知。用户将看到此通知。根据不同的情况,他们可能会对你的应用感到厌烦并卸载它。

最简单的解决方案是使用名为WorkManager的新的体系结构组件。您可以在这里查看文档:https://developer.android.com/topic/libraries/architecture/workmanager/

您只需定义扩展worker的worker类。

public class CompressWorker extends Worker {

    public CompressWorker(
        @NonNull Context context,
        @NonNull WorkerParameters params) {
        super(context, params);
    }

    @Override
    public Worker.Result doWork() {

        // Do the work here--in this case, compress the stored images.
        // In this example no parameters are passed; the task is
        // assumed to be "compress the whole library."
        myCompress();

        // Indicate success or failure with your return value:
        return Result.SUCCESS;

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)
    }
}

然后您可以安排您想要运行它的时间。

    OneTimeWorkRequest compressionWork = 
        new OneTimeWorkRequest.Builder(CompressWorker.class)
            .build();
    WorkManager.getInstance().enqueue(compressionWork);

简单!有很多方法可以配置worker。它支持重复作业,如果需要的话,你甚至可以做一些复杂的事情,比如链接。希望这能有所帮助。

其他回答

正如@kosev在他的回答中所说,你可以使用JobIntentService。 但是我使用了另一种解决方案——捕获IllegalStateException并将服务作为前台启动。 例如,这个函数启动我的服务:

@JvmStatic
protected fun startService(intentAction: String, serviceType: Class<*>, intentExtraSetup: (Intent) -> Unit) {
    val context = App.context
    val intent = Intent(context, serviceType)
    intent.action = intentAction
    intentExtraSetup(intent)
    intent.putExtra(NEED_FOREGROUND_KEY, false)

    try {
        context.startService(intent)
    }
    catch (ex: IllegalStateException) {
        intent.putExtra(NEED_FOREGROUND_KEY, true)
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent)
        }
        else {
            context.startService(intent)
        }
    }
}

当我处理意图时,我做了这样的事情:

override fun onHandleIntent(intent: Intent?) {
    val needToMoveToForeground = intent?.getBooleanExtra(NEED_FOREGROUND_KEY, false) ?: false
    if(needToMoveToForeground) {
        val notification = notificationService.createSyncServiceNotification()
        startForeground(notification.second, notification.first)

        isInForeground = true
    }

    intent?.let {
        getTask(it)?.process()
    }
}

如果服务通过扩展IntentService运行在后台线程中,你可以用JobIntentService替换IntentService, JobIntentService是Android支持库的一部分

使用JobIntentService的优点是,它在pre-O设备上表现为IntentService,在O或更高的设备上,它作为作业分派

JobScheduler还可以用于定期/按需作业。但是,确保处理向后兼容性,因为JobScheduler API仅从API 21可用

如果你在8.0上运行你的代码,那么应用程序将会崩溃。因此,在前台启动服务。如果低于8.0,使用以下命令:

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

如果在8.0以上,那么使用这个:

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

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

最好的方法是使用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());