0

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?

Harnirvair Singh
  • 583
  • 7
  • 23
Hithendra Nath
  • 173
  • 2
  • 10
  • 1
    Please show us the code of your job service. – zeeali Mar 07 '18 at 14:22
  • by force stop did you mean by clicking the 'force stop' in the app info page or swiping it away from recent apps? Coz I am not able to see my service running after clicking 'force-stop' from app info page. – rahulrvp Jun 03 '19 at 10:02

4 Answers4

1

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.

Harnirvair Singh
  • 583
  • 7
  • 23
  • Thanks for that. But, In my case I am force stopping my application. When I forcestop my application, other service I am using is being stopped. But, JobService which is taking care of scheduling job at regular intervals is not stopped. – Hithendra Nath Mar 06 '18 at 10:40
0

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

<service android:name="service" android:stopWithTask="true"/>
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
0

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();
Steve Miskovetz
  • 2,360
  • 13
  • 29
  • I have cancel logic in my service. The problem is when I force stop the application, no life cycle method is called. I am not sure where to call cancel (). – Hithendra Nath Mar 06 '18 at 17:43
  • Okay, I think I understand better. Why does recording fail? Are you holding/using references in your Job Service that went away on the force close? If so, you shouldn't because those could be taken out from under you too by Android, e.g. when memory is low. When using Job Scheduler, you are scheduling a task against Android's framework regardless of the state of the app. If the app was force closed, a new application context will be created if necessary, when the Job Scheduler calls the Job Service. Is Job Scheduler right for you? – Steve Miskovetz Mar 07 '18 at 01:20
  • A possible work around, haven't tried it myself, could be to have a public static boolean accessible to the Job Service set to false by default. When you schedule the job, set it to true. If you see it set to false when onStart() is called, you know your application's context is new, so cancel your jobs and return. But this could happen for other reasons like low memory instead of a force close. – Steve Miskovetz Mar 07 '18 at 01:22
  • Got it. Thanks. – Hithendra Nath Mar 07 '18 at 01:45
0

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.

Ali Izadyar
  • 140
  • 1
  • 2
  • 13