MyTaskService handles one time and repetitive tasks in the background thread.
Call startTaskService() in onCreate and start service in onActivityResult. This way TaskService will only start if a task is scheduled.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
startTaskService();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_GOOGLE_PLAY_SERVICES:
if (resultCode == Activity.RESULT_OK) {
Intent i = new Intent(this, TaskService.class);
startService(i);
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
private void startTaskService() {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isAvailable = api.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
} else if (api.isUserResolvableError(isAvailable) &&
api.showErrorDialogFragment(this, isAvailable, REQUEST_GOOGLE_PLAY_SERVICES)) {
// wait for onActivityResult call
} else {
Toast.makeText(this, api.getErrorString(isAvailable), Toast.LENGTH_LONG).show();
}
}
TaskService.scheduleOneOff(context);
One off task will start between 0 to 10 seconds.
TaskService.cancelOneOff(context);
TaskService.scheduleRepeat(context);
Repeating task will fire every 60 seconds.
TaskService.cancelRepeat(context);
TaskService.cancelRepeat(context);