JobInfo.Builder builder = new JobInfo.Builder(1,new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
builder.setPeriodic(3000);
Edited
MainActivity.java
public class MainActivity extends Activity {
private JobScheduler mJobScheduler;
private Button mScheduleJobButton;
private Button mCancelAllJobsButton;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
mJobScheduler = (JobScheduler) getSystemService( Context.JOB_SCHEDULER_SERVICE );
mScheduleJobButton = (Button) findViewById( R.id.schedule_job );
mCancelAllJobsButton = (Button) findViewById( R.id.cancel_all );
mScheduleJobButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
JobInfo.Builder builder = new JobInfo.Builder( 1,
new ComponentName( getPackageName(), JobSchedulerService.class.getName() ) );
builder.setPeriodic( 3000 );
if( mJobScheduler.schedule( builder.build() ) <= 0 ) {
//If something goes wrong
}
}
});
mCancelAllJobsButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View v ) {
mJobScheduler.cancelAll();
}
});
}
}
just need to change
new JobInfo.Builder(1, new ComponentName(this, UpdateDatabaseService.class))
to
new JobInfo.Builder( 1, new ComponentName( getPackageName(), JobSchedulerService.class.getName() ) )
builder.setPeriodic( 3000 );
will set JobInfo
in 3000 ms schedule and called after every 3 seconds.
JobSchedulerService.java
public class JobSchedulerService extends JobService {
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;
}
} );
@Override
public boolean onStartJob(JobParameters params ) {
mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) );
return true;
}
@Override
public boolean onStopJob( JobParameters params ) {
mJobHandler.removeMessages( 1 );
return false;
}
}
AndroidManifest.xml
<service android:name=".JobSchedulerService"
android:permission="android.permission.BIND_JOB_SERVICE" />
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/schedule_job"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schedule Job"/>
<Button
android:id="@+id/cancel_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel All"/>
</LinearLayout>
AlarmManager
has an undocumented lower limit of a one-minute polling period. Perhaps there is something similar at work here. IMHO, using eitherAlarmManager
orJobScheduler
for every-five-second work is inappropriate, as polling that quickly really should only be done by a foreground process that's already running. – Canonize