I want app to start a timer at certain point and run for four hour and is displayed in UI.
Now problem with using service is that it closes if app is closed. (Same with Handler)
And if I use system time than if someone changes system time, 4 hours extends
So is there a better way to implement such task?
You can make services not close when the activity (visible) is stopped. One way to guarantee it stays alive is to make it a foreground service (will show a notification). But if you don't make it a foreground service it will still last for some time unless your device is running out of battery for example.
In your case, to make it last four hours, perhaps use the foreground service alternative (showing a notification)
Foreground service
Vogella foreground service
AlarmManager should help see page http://developer.android.com/reference/android/app/AlarmManager.html
Related
I am trying to implement a timer with notification that cannot be swiped away by user. I want to continuously update the timer's time.
I know there are a lot of similar question online. But a lot of them discuss using handler, or a service. All of these won't run when my app is not being used by the user. AlarmManager doesn't work because i want to update every x millisecond.
Thanks.
Use a foreground service
A foreground service performs some operation that is noticeable to the
user. For example, an audio app would use a foreground service to play
an audio track. Foreground services must display a Notification.
Foreground services continue running even when the user isn't
interacting with the app.
There is a limit on how frequently you can update the notifications as well, every MS will cause problems.
I am designing an app that has weather info to display at home screen. I want to update weather every 10 minutes.
I have a issue to update UI when home activity is in background (another activity is open upon home activity)
some help but issue in updating UI:
Scheduling recurring task in Android
Please advice me if another way to do this. Thanks in advance.
Use AlarmManager:
Periodically executing background tasks
Executes even if application is not running
Used when long running task is not required forever
For most cases, setup a Scheduler that triggers a background service at regular intervals
Suggestion: Use IntentService in conjunction with the AlarmManager
Reference & link: AlarmManager - CodePath
I have gone through your question and I am glad to give you the appropriate answer based on my personal experience.
Well if you are interested in gathering the weather updates every 10 minutes even when the application is in background, then I suggest you to use Background Service.
You can't imagine the advantages of background service. There are three kinds of background service
Started Service
Intent Service
Bound Service
All of the above three has there own usages. But in your case i suggest you to use the Started service. This service will start when you will call the startService() method, and will stop when you will call the stopService() method. Using the background service over Alarm Manager is recommended, and implementation is also quite simple and you can go through this link to understand the background service.This service will also keep on running in background even when your app is in background and will also gather the weather data every 10 minutes.
This is similar to another question I asked where I was wondering how other apps like drupe dialer keep their service running forever when it is not in foreground. Because I've used job services, alarm manager, START_STICKY and everything else to try to keep my service alive but it always gets stopped by the OS.
You can run the service as "Foreground" and it will be not candidate to be killed by the system under low memory conditions. The gotcha is that you will need to show that behavior to the user with a notification. This is the way that music players uses to go background and alive when you start another apps.
Foreground Services
The app you mentioned (Drupe Dialer) is a Dialer. It might be listening to broadcasts and turning the service up every time by checking whether its up.
To answer your question, you need to keep the service started as START_STICKY to make it restart after the OS kills it. But AlarmManager does not work at device sleep states, and doze will stop it from running in the background nevertheless.
The real question is; WHY you want to keep it running? That might answer your question on HOW you want to do that.
If its a communication type app, you will need to use something like
GCM.
If its running background work based on some event, you might
want to start the service inside the BroadcastReceiver.
etc.
it depends on what app you're writing.
I am creating a notification app, which will alert user after they set a reminder notification. My current implementation logic is:
Create a Service, which starts running in background when user opens their app.
In onCreate() method of service, I am implementing a Timer task which will repeat after 5000ms interval and will call a method, which will check all reminders set by user and notify user if any reminder is set for current time.
I use broadcast receiver to run the service on Boot_Completed event, if in case user turns off their phone.
This implementation is working good, I have faced no issues with it, but my concern is that is this a good practice? Keeping in mind that service running and checking every 5 secs will consume battery. Also if user turns on Stamina Mode, Power saving mode or any such mode, will OS kill my service. Is there anything I can do to give priority to my Service not to be killed.
If there is any other more efficient way to implement this sort of task, I want to implement that in my project.
Looking forward for suggestions.
Much Appreciated.
best approach is wakeful intent service with alarm receiver as mentioned here
https://github.com/commonsguy/cwac-wakeful
all good but use AlarmManager.setRepeating() as timer. the intent come even if your app killed.
I have implemented a background Service that is running in the same process as my app. My problem is that when the app is closed (tab on windows button, then swipe app to the left to close it), it causes the whole process to close and the Service to stop working as well.
On the OnStartCommand() callback in the background service, I return START_STICKY.
It takes about 5 seconds for the process to appear as closed (0 processes and 1 service) on app settings page as below
Currently to overcome this problem, in my activity OnDestroy() callback I set an alarm-manager to start my service again after 10 seconds -giving enough time for process to fully close before opening it again. This solution works to an extent, but it's not what I want. What I really want is for my Service not to close from the first place.
I have looked at the option of making the Service in a separate process, however, I then found major difficulties in using variables and functions on the service class and stuff since they are now in separate memory stacks.
I know about the foreground service solution but unfortunately having the that notification stick there all the time is very intimidating.
Is there any good solution to keep the service running even when the user closes the app ?