Stopwatch service for updating notification - android

I am working on time tracking app. I need to track time and display it in activity and notification in minutes and seconds live. In order to display live time in notification I need some kind of service that updates the notification each second eventually sends broadcast for updating the stopwatch in activity. The question is how to implement the service.
Should I use something like ACTION_SCREEN_OFF/ACTION_SCREEN_ON to start and stop the service? What kind of timer should I use for example JobScheduler?
Thank you all for any response :)

I actually discovered that I dont need a service to update the notification. I can use Chronometer widget that does all the magic with counting seconds.

Related

How to continuously update notification even when app is not running?

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.

Create Reminder Notification for Android

I know how to create scheduled notification in Android by using alarm service but what I want to do now is to create notification in more frequent way.
For example, I want to push notification for 8 hours at the interval of 20 mins. In this case, is it efficient to use alarm service or timertask will be the better choice?
No matter which method, I wish to able to cancel it in the halfway. Thanks.
Timertask starts new thread and works in it. So if your app will work in background and your app will be closed by android, you won't receive any notification. AlarmManager provides access to the system alarm services. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. (link). So it will start your app even if it was closed. And you have to understand how you app will work with this notifications. If it works only while user works in app, you can use timertask, but if it has to work in background(for example you will receive notification even if user doesn't work with a phone/tablet), it will be better use alarmanager. Hope it helps.

Best practice for scehduled background task Android

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.

Android how to implement timer that doesn't stop if app closes

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

Updating weather info, should I use alarm and/or a service?

I am having some design techniques about How shall I schedule a code to retrieve the weather info?
Should I use alarms to retrieve the weather each 10 minutes?
And do I need to run a service for this? Or just put the code in the Broadcastreceiver and start when the alarm fired?
This is a good question. Yes, you will need to put this code in either a service or broadcastreceiver because when and activity loses focus (meaning the user is using a different app or the phone is asleep) they pause and/or close. However, i have no experience with either Services or Broadcastrecievers, so that is as much as i can tell you.

Categories

Resources