Java – job scheduling setrequiresdeviceidle (true) and setperiodic () do not work
•
Android
I'm developing an application to execute the background task and check whether the device is idle in 10 seconds, but some how to trigger every 10 seconds without checking whether the device is idle
MainActivity.java
private static final int JOB_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mJobScheduler = (JobScheduler) getSystemService(this.JOB_SCHEDULER_SERVICE);
construct_job();
}
private void construct_job(){
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, new ComponentName(getPackageName(), MyJobService.class.getName()))
.setPeriodic(10000).setRequiresdeviceidle(true);
int code = mJobScheduler.schedule(builder.build());
if( code <= 0 ) {
//If something goes wrong
Log.v("SOMETHING", "GOES WRONG");
}
}
MyJobService.java
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
Log.v("OnStart", "onStartJob-h");
//Job Will Be Started Here
mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) );
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.v("JOB","STOPPED");
mJobHandler.removeMessages( 1 );
return false;
}
private Handler mJobHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage( Message msg ) {
Toast.makeText( getApplicationContext(),
"JobService task running", Toast.LENGTH_SHORT )
.show();
jobFinished( (JobParameters) msg.obj, false );
return true;
}
});
}
AndroidManifiest.xml
<service
android:name=".MyJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
Yes, everyone knows what I did wrong? Please help?
resolvent:
Adding. Setrequirementsenforced (true) may solve your problem
View document: multiple requirements can be set for a job, but not all requirements need to be considered by the basic {@ link jobapi}. If the requirements are enforced, the device status will be checked before the job runs. If at least one requirement is not met, the job will be rescheduled without running
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码