Hithendra Nath
Hithendra Nath

Reputation: 173

Why job service is still running when application is force stopped?

In my audio recording application, I created a job service which will schedule a job of recording at scheduled frequency intervals. Once I schedule the job to record for every 5 minitues(example), after few recordings, when I force stop the application, I could see job service is still running and tries to record and fails. All other services in the application are stopped.

When I force stop the application why job service is still running? Do I need to take any extra care to stop the job service?

Upvotes: 0

Views: 3612

Answers (4)

Ali Izadyar
Ali Izadyar

Reputation: 220

I know Its too late but I post my answer for future refers:

Job Scheduler will continue working for at least 15 minutes.(Even if you clear the app from recent by swipe or use force stop)

So if you want your service to stop working after application is closed, you should use Bound Service.

Bound Service would continue working(Even in background) until application is closed.

Upvotes: 0

Steve Miskovetz
Steve Miskovetz

Reputation: 2510

Your job is a periodic job, correct? Have you tried explicitly cancelling the job with its respective id?

    JobScheduler scheduler = (JobScheduler)
            mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    for (JobInfo jobInfo : scheduler.getAllPendingJobs()) {
        if (jobInfo.getId() == jobID) {
            scheduler.cancel(jobID);
            Log.i(TAG,"Cancelled Job with ID:" + jobID);
        }
    }

Or you can try canceling all jobs that have been registered with the JobScheduler by this package.

    JobScheduler scheduler = (JobScheduler)
            mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    scheduler.cancelAll();

Upvotes: 0

Dinith Rukshan Kumara
Dinith Rukshan Kumara

Reputation: 680

use below code for your service in manifest file. This will stop service with its task.

<service android:name="service" android:stopWithTask="true"/>

Upvotes: 0

Harnirvair Singh
Harnirvair Singh

Reputation: 593

A LOT of Android apps run a small service in the background that allows inter-connectivity with other apps/services and to allow notifications when you are not using the app (Facebook, text messages, email, ads, etc). Be clear, there is a difference between a SERVICE running in the background and an APP running in the background after you open it.

So you can prevent it by destroying the service when the user closes the app, through the following two ways.

First, from within the Service class, call:

stopSelf();

OR

Second, from within another class, like your MainActivity for example:

Intent i = new Intent(this, ServiceName.class);
stopService(i);

Both of these will stop your service. Make sure you are returning START_NOT_STICKY so that the service doesn't start back up again.

Upvotes: 1

Related Questions