Android JobScheduler-无法创建持久性作业 [英] Android JobScheduler - can't create a persistent job

查看:80
本文介绍了Android JobScheduler-无法创建持久性作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用Android Lollipop随附的新的JoScheduler API.到目前为止,我已经成功地成功创建了作业,并且延迟了6000毫秒,并且没有网络需求,没有问题.

I am trying out the new JoScheduler API that has come with Android Lollipop. I have so far managed to successfully create and have a job run with a delay of 6000 milliseconds with no network requirements without an issue.

但是,我刚刚尝试通过使用setPersisted(true)函数来坚持相同的工作.调用job build()函数后,现在却无法说明我需要清单文件中的RECEIVED_BOOT_COMPLETED权限.

However, I have just tried to persist the same job via using the setPersisted(true) function. As soon as the job build() function is called it now fails saying that I need the RECEIVED_BOOT_COMPLETED permission in the Manifest file.

但是我添加了权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  package="com.android" >

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

在添加作业之前,我什至添加了以下代码,以查看应用程序是否已注册权限:

I have even added the following code before the job is added to see if the app has the permission registered:

PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(Permission.RECEIVE_BOOT_COMPLETED, 
context.getPackageName());
if (hasPerm == PackageManager.PERMISSION_GRANTED)
{
   // Goes into here every time
}

但是,在构建作业时,出现以下错误:

However, when the job is built I get the following error:

java.lang.IllegalArgumentException: Error: requested job be persisted without holding RECEIVE_BOOT_COMPLETED permission.

我用于创建作业并将其添加到JobSchedular的代码:

My code to create and add the job to the JobSchedular:

ComponentName serviceComponent = new ComponentName(getApplicationContext(), MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(1, serviceComponent)
        .setMinimumLatency(6000)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setPersisted(true);
JobScheduler jobScheduler = (JobScheduler) getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());

我的JobService清单声明:

My JobService manifest declaration:

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

因此,我想知道我是否在做其他任何人都可以发现的错误操作.我唯一要注意的是,它确实在IntentService中运行,因此我想知道这是否可能是JobScheduler找不到权限的原因.

So I am wondering if I am doing something else wrong that anyone can spot. The only thing i'll note incase it does make a difference is that the code is run in an IntentService so I am wondering if this might be a reason why the JobScheduler can't find the permission.

推荐答案

我遇到了同样的问题,但是后来意识到在添加启动许可后我没有重新安装我的应用程序.因此,该应用在安装后再也没有机会请求所需的权限.

I had the same issue, but then realized that I hadn't re-installed my app after adding the boot permission. So the app never got an opportunity to request the required permission when it was installed.

一旦我卸载并重新安装了可以正常运行的应用程序.

Once I uninstall and re-installed the app it works.

注意:我的理解是Apps需要在安装时获得其许可.但是,我在这里有待纠正.

Note: My understanding is that Apps need to get their permission at install time. However I stand to be corrected here.

这篇关于Android JobScheduler-无法创建持久性作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆