Skip to content

Instantly share code, notes, and snippets.

@JosiasSena
Created March 10, 2017 22:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JosiasSena/503e05a886f5e03e23457489471926de to your computer and use it in GitHub Desktop.
Save JosiasSena/503e05a886f5e03e23457489471926de to your computer and use it in GitHub Desktop.
Job Scheduler Code sample
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.josiassena.jobscheduler">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".MyJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
</manifest>
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scheduleJob();
}
});
}
private void scheduleJob() {
final JobScheduler jobScheduler = (JobScheduler) getSystemService(
Context.JOB_SCHEDULER_SERVICE);
// The JobService that we want to run
final ComponentName name = new ComponentName(this, MyJobService.class);
// Schedule the job
final int result = jobScheduler.schedule(getJobInfo(123, 1, name));
// If successfully scheduled, log this thing
if (result == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Scheduled job successfully!");
}
}
private JobInfo getJobInfo(final int id, final long hour, final ComponentName name) {
final long interval = TimeUnit.HOURS.toMillis(hour); // run every hour
final boolean isPersistent = true; // persist through boot
final int networkType = JobInfo.NETWORK_TYPE_ANY; // Requires some sort of connectivity
final JobInfo jobInfo;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
jobInfo = new JobInfo.Builder(id, name)
.setMinimumLatency(interval)
.setRequiredNetworkType(networkType)
.setPersisted(isPersistent)
.build();
} else {
jobInfo = new JobInfo.Builder(id, name)
.setPeriodic(interval)
.setRequiredNetworkType(networkType)
.setPersisted(isPersistent)
.build();
}
return jobInfo;
}
}
public class MyJobService extends JobService {
private static final String TAG = MyJobService.class.getSimpleName();
@Override
public boolean onStartJob(final JobParameters params) {
HandlerThread handlerThread = new HandlerThread("SomeOtherThread");
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
Log.e(TAG, "Running!!!!!!!!!!!!!");
jobFinished(params, true);
}
});
return true;
}
@Override
public boolean onStopJob(final JobParameters params) {
Log.d(TAG, "onStopJob() was called");
return true;
}
}
@leandro1995
Copy link

hello I only have one query I have been seeing examples of several and always in the method jobFinished () instead of placing a true place a false and I have seen several examples of different people wanted to know if you could explain that part because true and not false only ,Thank you

@frestoinc
Copy link

@leandro1995,

imo you set true if you don't need to reschedule the task, else set it to false such as in exception so that system will reschedule accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment