Skip to content

Instantly share code, notes, and snippets.

@adnaanaeem
Forked from sivabe35/AndroidManifest.xml
Created September 24, 2019 07:25
Show Gist options
  • Save adnaanaeem/11d8553bca18d43d19d3c2141794607c to your computer and use it in GitHub Desktop.
Save adnaanaeem/11d8553bca18d43d19d3c2141794607c 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment