Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback to jobservice from intentservcie

I have a jobscheduler that triggers onStartjob of Jobservice. In onStartJob, I start an intentservice to do the work. After the work is done, I want intentservice to do a callback to jobservice so that onjobfinished can be called. How can I do a callback to JobService?

like image 615
user1270175 Avatar asked Jul 28 '16 22:07

user1270175


People also ask

What is JobService?

android.app.job.JobService. Entry point for the callback from the JobScheduler . This is the base class that handles asynchronous requests that were previously scheduled. You are responsible for overriding JobService#onStartJob(JobParameters) , which is where you will implement your job logic.

What is on start job called?

OnStartJob is called by the Android system when it starts your job. However, onStopJob is only called if the job was cancelled before being finished (e.g. we require the device to be charging, and it gets unplugged).

Why is there a need for JobIntentService?

If you think that user don't need to know about your work you can use JobIntentService without notification. You don't need to worry about Doze mode if user is actively using your app. For example, user turned screen off while you are computing something. Your task is finished and you want to run background service.

What is JobIntentService in Android?

JobIntentService is the modern way of using the background service to get our task done. The IntentService does not work well with Oreo devices and above. That is it made our application crash in the Oreo devices. To overcome this problem android introduced JobIntentService that works well in all the devices.


1 Answers

You can create BroadcastReceiver and register it in your Jobservice, in onStartJob() method, using some ACTION constant (for example ACTION_DOWNLOAD_FINISHED). This receiver will delegate all work to onJobFinished() method:

public static final String ACTION_DOWLOAD_FINISHED = "actionDownloadFinished";

private BroadcastReceiver downloadFinishedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) { 
        context.unregisterReceiver(this); //Unregister receiver to avoid receiver leaks exception
        onJobFinished();
    }
};

public void onStartJob() {
    IntentFilter filter = new IntentFilter(ACTION_DOWNLOAD_FINISHED);        
    //Use LocalBroadcastManager to catch the intents only from your app
    LocalBroadcastManager.getInstance(this).registerReceiver(downloadFinishedReceiver , filter);

    //other job starting stuff...
}

Then, after the intent service has ended it's work, you can send broadcasting intent with ACTION_DOWNLOAD_FINISHED action from it:

// ...downloading stuff
Intent downloadFinishedIntent = new Intent(Jobservice.ACTION_DOWNLOAD_FINISHED);
//Use LocalBroadcastManager to broadcast intent only within your app
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

When the job of the intent service is finished, it sends broadcasting intent that is catched by the receiver registered in the Jobservice. Receiver then invokes the onJobFinished() method.

You can find the details there: https://developer.android.com/training/run-background-service/report-status.html

like image 127
Edonoxako Avatar answered Sep 19 '22 17:09

Edonoxako