Android java.lang.IllegalArgumentException:没有这样的服务ComponentInfo JobScheduler

问题描述 投票:16回答:3

我试图创建一个简单的JobScheduler作业,看看它是如何工作的。但我一直在运行时遇到这个异常,我无法弄明白,因为我一步一步地按照指南。

这是我的电话:

        ComponentName componentName = new ComponentName(getApplicationContext(), TestService.class);
    JobInfo jobInfo = new JobInfo.Builder(1, componentName).setPeriodic(300000)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build();

    JobScheduler tm =
            (JobScheduler) getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
    tm.schedule(jobInfo);

TestService不执行任何操作,然后扩展JobService。

android android-jobscheduler
3个回答
40
投票

您需要将权限android.permission.BIND_JOB_SERVICE添加到AndroidManifest.xml中

...
<service android:name=".TestService"
     android:permission="android.permission.BIND_JOB_SERVICE"
     android:exported="true"/>
...
</application>


2
投票

尝试清洁项目。这是我的情况。


0
投票

我的问题是我的服务被定义为静态内部类。一旦我将服务类移动到它自己的Java文件中,问题就解决了

© www.soinside.com 2019 - 2024. All rights reserved.