1

I want to run a repetitive task which do some background task, it do not need to show its progress or any UI update. but it must run even app goes swiped with minimum resource usage.(battery, CPU, etc etc), please suggest me some way. how to do that.

This background task must be start from application and stooped from application only.

My Main Problem:

App has two State:1. Start App 2.Stop App

  • in "Start App State" i need to check whether location service is enable or not, even app swiped, so i write Service and Timer in it to check repetitively that Location Service is enable.

  • in "Stop App State" i need to stop the timer and stop the service.

now Problem is that Timer is not running sometime, so it do not set flag in preferences.

List of Solution i had tried:

  1. BroadCastReciever with "PROVIDERS_CHANGED_ACTION" event Listened, but it do not call receiver everytime when user start-stop the location service,

  2. so i write Service with Timer to check location service is enable or disable. but Timer do not run everytime, as i checked in two device(Samsung S5 & Google Nexus 6).

  3. so i remove service and i use AlaramManager with BroadcastReciever it can run as Expected but i need to take care about device Resources(Bettry and CPU) becuase it will execute every 1 sec.

so Advice me.

Thanks in advance

JK Patel
  • 858
  • 8
  • 22
  • create a `Service` for task – Iamat8 Nov 27 '15 at 04:56
  • I would suggest you should make use of service class and create a thread inside the service class. As by default the service would be attached to application main thread. And Try using timer task to repeat the process in back ground. – Nitin Mesta Nov 27 '15 at 04:56
  • I had created a service which can be start and stop from code, i use TIMER with scheduling to do repeat a task, but timer is not trust-able to run every-time, it stooped sometime in some device only. i found that in Samsung Galaxy S5. and my service run successfully in Nexus 6 – JK Patel Nov 27 '15 at 05:03
  • Android is simply not designed to support this type of use case, so at best you will approximate it under some conditions, but it will never be reliable and you should expect to find ongoing issues on various devices and versions. Your effort would be better spent on a more practical project. – Chris Stratton Nov 27 '15 at 05:18

3 Answers3

1

but it must run even app goes swiped with minimum resource usage.

You'll have to take care of a few things:

  1. In android manifest:

    <service android:name=".MyService" android:stopWithTask="false"/>

Note: some devices like samsung etc stop services, even if configured as per android guidelines.

  1. Make service foreground, so that android does not makes it a candidate for stopping when resources are low.

  2. Set up the service to automatically start after re-boot if it was running before.

  3. On newer android versions, a feature called doze is going to kick-in. You'll have to test your app for that case too.

Finally, if you want to try latest API, there is a new Job Scheduling API available that takes care of most of the things.

Community
  • 1
  • 1
S.D.
  • 29,290
  • 3
  • 79
  • 130
0

If you don't want your process to be killed in the background, then you should use a Foreground Service.

Eric B.
  • 4,622
  • 2
  • 18
  • 33
  • Thanks for your answer, I want a repetative task also, My original problem is that, i use TIMER to do repetative task, but timer is not running sometime, so my repetative task is not performing everytime, i need to run repetitive task in background with no UI Update, can foregroundService will do that ? – JK Patel Nov 27 '15 at 05:12
  • 1
    You can use AlarmManager to work every second to make sure your service's work is done, even when the screen is turned off. Or you could use a TimerTask with PowerManager. – Eric B. Nov 27 '15 at 05:17
0

Create a service and call Alarm Manager in it(using pendingIntent) setting the repetetion time to 1sec.Then u can see ur service will be called for every 1sec even ur app is not running.

srinivas
  • 72
  • 1
  • 9
  • i do not know more about alaram manager, but as i read about it, i know that it add wake lock in CPU processing. so Alarammanager required more resources compare to Timer ? – JK Patel Nov 27 '15 at 05:06
  • Thanks for your answer, I start alarammanager with BroadcastReciever, it executing even my app goes swiped, but i need to take care about resources of android device, so AlaramManager with BroadcastReciever can take less resources than AlaramManager with Services ? any idea about it ? – JK Patel Nov 27 '15 at 05:09
  • That's a nice idea!!!u can go ahead if u want to check "all the time" becoz I think that u could not stop it.And i also want to tell u one thing that I have read in **Bignerd_Ranch guide** that " broadcast receiver has a very short life,upon receiving an intent it will invoke something and it will die that is the work of receiver" – srinivas Nov 27 '15 at 05:29
  • 1
    nice thinking about the resources patel – srinivas Nov 27 '15 at 05:30