我创建了一个JobScheduler,可以根据日期和时间在我的应用中为用户提供一些功能。
但是发现约伯并没有安排在我期望的确切时间。间隔后,工作都没有停止。根据我的研究,所有时间安排都取决于setOverrideDeadline
,setMinimumLatency
和durationToSet
发现开始和停止的延迟时间为30-55秒,这在每次试验中都不一致。
我已经重新调整了我的应用功能,引用了disable CPU scaling
以下是我将根据预期日期时间安排的代码示例。 (仅提供SO的基本步骤)
String startDate = "2018-01-05";
String endDate = "2018-01-05";
String startTime = "11:10"
String endTime = "11:30"
在将开始日期与开始时间组合后,按以下格式转换日期时间,如下所示。与结束时间类似的结束日期。
String dateTimeStart = startDate + " " + startTime;//For start date time
String dateTimeEnd = endDate + " " + endTime;//For end date time
将使用以下逻辑计算长值作业的开始和结束时间。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long timeInMilliseconds = 0;
try {
Date mDate = sdf.parse(dateTimeStart);
timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
Log.e(TAG, e.toString());
} catch (Exception e) {
Log.e(TAG, e.toString());
}
现在根据此输入,我将使用this sample为每个作业创建一个唯一ID,并安排作业,如下所示。
public void scheduleJob(int jobID, long startMillSec,long endMilliSec) {
JobInfo.Builder builder = new JobInfo.Builder(jobID, mServiceComponent);
PersistableBundle extras = new PersistableBundle();
long currTimeMillSec= System.currentTimeMillis();
long minLatency =0;
//if time already crossed start time, then start time is current time itself
if(currTimeMillSec>=startMillSec){
minLatency=0;
}else{
minLatency=startMillSec-currTimeMillSec;
}
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE);
builder.setOverrideDeadline(startMillSec);
builder.setMinimumLatency(minLatency);
builder.setRequiresDeviceIdle(false);
builder.setRequiresCharging(false);
builder.setPersisted(true);
long durationToSet=0;
//if time has passes, keep the delta as the duration, or else keep the full duration received from backend
if(currTimeMillSec>=startMillSec){
durationToSet= endMilliSec-currTimeMillSec;
}else{
durationToSet=endMilliSec-startMillSec;
}
//Set duration for job stopping
extras.putLong("WORK_DURATION_KEY", durationToSet);
builder.setExtras(extras);
JobScheduler tm = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
tm.schedule(builder.build());
}
以下是我的JobService类
public class AppJobScheduler extends JobService
{
@Override
public boolean onStartJob(final JobParameters params) {
sendMessage(MSG_START, params.getJobId());
long duration = params.getExtras().getLong(WORK_DURATION_KEY);
// Uses a handler to delay the execution of jobFinished().
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
sendMessage(MSG_STOP, params.getJobId());
jobFinished(params, false);
}
}, duration);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
sendMessage(MSG_STOP, params.getJobId());
return false;
}
private void sendMessage(int messageID, @Nullable Object params) {
Message m = Message.obtain();
m.what = messageID;
m.obj = params;
try {
mActivityMessenger.send(m);
} catch (RemoteException e) {
Log.e(TAG, " Error passing service object back to .");
}
}
}
其他参考资料是AtomicInteger和reference one
非常感谢有关此方面的任何帮助!