实际上,在android中,无论应用是否处于活动状态,我都希望在1分钟后运行我的工作,但是我不想在大约3,4分钟后运行我的工作,请给我建议我如何根据给定的条件运行它时间。
谢谢。
这是我的代码:
ComponentName componentName = new ComponentName(MainActivity.this, JobSchedularClass.class);
jobInfo = new JobInfo.Builder(code, componentName)
.setMinimumLatency(1000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setOverrideDeadline(1000)
.setPersisted(true)
.build();
jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
}
public void Startjob(View view) {
int code_result = jobScheduler.schedule(jobInfo);
Toast.makeText(this, " Job scheduling...", Toast.LENGTH_SHORT).show();
if (code_result == JobScheduler.RESULT_SUCCESS) {
Toast.makeText(this, " Job scheduling done...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " Job scheduling failed...", Toast.LENGTH_SHORT).show();
}
}
public void Stopjob(View view) {
jobScheduler.cancel(code);
Toast.makeText(this, " Job cancel...", Toast.LENGTH_SHORT).show();
}
这是我的求职服务类代码:
public class JobSchedularClass extends JobService {
private static final String TAG = "JobSchedularClass";
private BackgroundJob backgroundJob;
@Override
public boolean onStartJob(final JobParameters params) {
Log.d(TAG, "onStartJob: ");
backgroundJob=new BackgroundJob(){
@Override
protected void onPostExecute(String s) {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
jobFinished(params,true);
}
};
backgroundJob.execute();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "onStopJob: ");
backgroundJob.cancel(true);
return false;
}
}
这是我的异步任务代码:
public class BackgroundJob extends AsyncTask<Void,Void,String> {
private static final String TAG = "BackgroundJob";
@Override
protected String doInBackground(Void... voids) {
Log.d(TAG, "doInBackground: ");
return "BackGround Job is running";
}
}