Skip to content

sinant/Gcm-Task-Service-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

GcmTaskService Example

MyTaskService handles one time and repetitive tasks in the background thread.

Starting Task Service

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();
    }
}

One off task

Create

TaskService.scheduleOneOff(context);

One off task will start between 0 to 10 seconds.

Cancel

TaskService.cancelOneOff(context);

Repeating Task

Create

TaskService.scheduleRepeat(context);

Repeating task will fire every 60 seconds.

Cancel

TaskService.cancelRepeat(context);

Canceling All Active Tasks

TaskService.cancelRepeat(context);

About

GcmTaskService handles one time and repetitive tasks in the background thread.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages