android - JobScheduler/JobInfo 的显式唤醒锁
问题描述
我正在重构这段代码:
https://github.com/r3gis3r/CSipSimple/blob/master/src/com/csipsimple/utils/TimerWrapper.java
它使用 AlarmManager 来安排计时器。我正在按照 Google 的建议重构代码以使用JobScheduler:
/**
* @param entryId
* @param latencyMs
* @brief Handler for schedule
*/
private synchronized int handleSchedule(int entryId, int latencyMs) {
// clamp to minimum latencyMs
latencyMs = (latencyMs < TIMER_MIN_LATENCY_MS) ? TIMER_MIN_LATENCY_MS : latencyMs;
// Cancel previous registrations for the entryId
mJobScheduler.cancel(entryId);
int existingReg = mScheduleEntries.indexOf(entryId);
if (existingReg != -1) {
mScheduleEntries.remove(existingReg);
mScheduleTimes.remove(existingReg);
}
long fireTimeMs = SystemClock.elapsedRealtime() + latencyMs;
Log.v(TAG, "Schedule timer " + entryId + " in " + latencyMs + "ms @ " + fireTimeMs);
ComponentName componentName = new ComponentName(mContext, PTAPITimerJobService.class);
PersistableBundle persistableBundle = new PersistableBundle();
persistableBundle.putInt(EXTRA_TIMER_ENTRY, entryId);
persistableBundle.putLong(EXTRA_TIMER_EXPIRATION, fireTimeMs);
final JobInfo.Builder jobInfoBuilder = new JobInfo.Builder(entryId, componentName).setExtras(persistableBundle);
jobInfoBuilder.setMinimumLatency(latencyMs);
// TODO - set wakelock criteria
JobInfo jobInfo = jobInfoBuilder.build();
mJobScheduler.schedule(jobInfo);
mScheduleEntries.add(entryId);
mScheduleTimes.add(fireTimeMs);
return 1;
}
上述方法等效于来自 CSipSimple 的粘贴链接中的 doSchedule。从JobInfo的文档中,有一些方法,如:setRequiredNetworkType、setRequiresDeviceIdle 用于设置各种条件。
问题:在 JobInfo 中提及这些标准是否足够,或者我们需要管理一个显式的 WakeLock(如 AlarmManager 案例所示)?
定时器的 WakeLock 类型:
public class WakeLockTypes {
public static final int LOCK_TYPE_NONE = 0x0;
public static final int LOCK_TYPE_WIFI = 0x1; // Keep the Wi-Fi radio awake
public static final int LOCK_TYPE_WIFI_MULTICAST = 0x2; // Allow receiving of Wifi Multicast packets
public static final int LOCK_TYPE_POWER_WAKE = 0x4; // Keeps the device CPU on
public static final int LOCK_TYPE_SCREEN_DIM_WAKE = 0x8; // Keeps the screen on (dim)
public static final int LOCK_TYPE_DEFAULT = (LOCK_TYPE_SCREEN_DIM_WAKE|LOCK_TYPE_WIFI);
}
解决方案
JobScheduler
将在您的工作运行时让您的设备保持唤醒状态,因此您不需要自己的WakeLock
.
推荐阅读
- json - POSTMAN - Visalize 定制的 JSON 文件
- python - importlib 不适用于子目录中的文件
- java - 如果请求来自,是否可以获得域或 IP 地址?
- iframe - DTM 跟踪 iframe 内的非 youtube 视频
- vue.js - 我如何将 Newrelic 与 Nuxt.js 一起使用
- java - JSON 解析错误:无法反序列化 START_ARRAY 令牌中的实例
- java - JUnit:如何通过构造函数创建具有依赖注入的测试类
- c# - 如果未经授权,如何本地化错误消息
- r - R 中的 dplyr 包:subset() 函数按月对数据帧进行子集化;错误 -<0 行>(或 0 长度行名称)
- c++ - 对字符串的真实条件检查