gpt4 book ai didi

java - 在锁定屏幕的情况下运行应用程序时,JobScheduler 作业无法启动

转载 作者:太空狗 更新时间:2023-10-29 14:41:21 24 4
gpt4 key购买 nike

我一直在尝试制作一个适用于 Android 打瞌睡功能的应用程序,但我遇到的问题是,当手机 (Android 6) 最近锁定屏幕并且我使用 Android Studio 运行该应用程序时,作业永远不会执行。阅读文档并看到这个 Doze 图后: enter image description here我希望有一个时间窗口来发出 HTTP 请求来更新我的应用程序,但我已经等待了至少 15 分钟,但从未发生过。

正在创建作业并在 onCreate 方法中安排,如下所示:

ComponentName componentName =
new ComponentName(getApplicationContext(), ConnectJobService.class);
jobInfo = new JobInfo.Builder(33, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build();
int resultCode = jobScheduler.schedule(jobInfo); //resultCode == JobScheduler.RESULT_SUCCESS > true

这是我的 list

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

<application
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ConnectJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

最佳答案

在 Android 6 上,关闭屏幕后不会进入打盹模式,而是 after an unspecified amount of time .

If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode.

因此,我怀疑这不是问题所在。一种可能的选择是 JobScheduler 受到 CPU 在屏幕关闭几秒钟后进入低功耗模式的影响。 (相关:Keeping the Device Awake)

根据我的经验,即使在设备解锁且屏幕打开的情况下安排任务也可能会延迟几秒钟,因此您遇到的情况可能与此类似。

我会尝试使用退避期和截止日期。请注意,默认退避策略是指数:

ComponentName componentName =
new ComponentName(getApplicationContext(), ConnectJobService.class);
jobInfo = new JobInfo.Builder(33, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setOverrideDeadline(5000L)
.setBackoffCriteria(500L, JobInfo.BACKOFF_POLICY_LINEAR)
.build();
int resultCode = jobScheduler.schedule(jobInfo); //resultCode == JobScheduler.RESULT_SUCCESS > true

注意 default initial backoff是 30000ms 和 maximum是 18000000 毫秒(这是 5 小时!)。

关于java - 在锁定屏幕的情况下运行应用程序时,JobScheduler 作业无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47835155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com