Android JobScheduler

编程入门 行业动态 更新时间:2024-10-21 03:44:47
本文介绍了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="schemas.android/apk/res/android" xmlns:tools="schemas.android/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

本文发布于:2023-11-27 09:19:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   JobScheduler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!