Using AlarmManager for periodic task Android - java

I am new to Android. I want to develop an app that is going to do something every 20 minutes: if the user is in the app, they just get a message, else, the app will set a dialog and if the user accepts that dialog, the app will open and the user will get that message.
I have searched how to do that and ended up using alarm manager and everything went fine. However, the question is that if using alarm manager is good for this situation. If not, why? And what is the solution? I had read somewhere that work manager is also good.

WorkManager will not be useful in a case like this when device enters Doze mode.
WorkManager adheres to power-saving features and best practices like Doze mode
I have seen that even after white listing the app, (removing from battery optimisation), if device is left unused, stationary, unplugged, WorkManager work is delayed until the next maintenance window, sometimes hours of delays.
AlarmManager can be used but documentation recommends
Exact alarms should only be used for user-facing features. Learn more about the acceptable use cases for setting an exact alarm.
FCM is another option that could be considered in doze mode.
Edit: WorkManager is definitely recommended for persistent onetime or periodic works which are not time sensitive, where combination of constraints can be applied.

Related

Make Chathead bubble run forever in android

Hi I am trying to make a chathead bubble, like the one facebook has, for an app in android studio. I have been able to successfully display the bubble using Overlay and make it a service which continues to run even after the app is closed (not killed). However when I open another app or if I dont use my phone for more than 10 minutes, the chathead bubble disappears, unlike Facebook's bubble. How can I go about making the bubble display on the home screen and other apps for a longer period of time(potentially forever)?
For context, I used https://www.androhub.com/android-floating-widget-like-facebook-messenger-chat-head/ to make the bubble, using a View and a Service.
Thanks in advance
Your app's service will get killed by the android system to save battery. There is no exact and easy way to do that. You have to implement multiple methods to do that.
Use Foreground Service(You may have already doing this but just in case if not.)
Ask for permission to restrict Battery Optimization.
In some devices like Xiaomi and Vivo, you need special permission to always run in the background ask for that.
Ask the user to lock the app in recent tab so it won't get killed by the system.
If you are implementing the service, override onStartCommand() and return START_STICKY as the result. It will tell the system that even if it will want to kill your service due to low memory, it should re-create it as soon as memory will be back to normal.
If you are not sure 1st approach will work - you'll have to use AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html . That is a system service, which will execute actions when you'll tell, for example periodically. That will ensure that if your service will be terminated, or even the whole process will die(for example with force close) - it will be 100% restarted by AlarmManager.

Android - What is the best practice for background tasks?

my app lets user create scheduled task.
Example: Tomorow show this notification => this notification leads to this activity etc.
What is the best practice to do this? Do I need to use some background task manager? How can I do this so it will work even when App is not running?
Facebook is doing something similar, when it is not running it still checks web for useful info for you.
Can somebody point me to some tutorial or get me some example? Thank you
There are several solutions that are recommended by Android framework team
Firebase JobDispatcher
GCM Network Manager, codelab
Job Scheduler api 21+
For showing a notification at a known point in the future (either a specific time or X hours from now) use AlarmManager to set an alarm.
For getting notifications from a server, generally you'd use push messages from the server. Although polling the server on an alarm would also work.

What is the correct way to use repeating alarms for short intervals in Android 5.0+?

So I need to send coordinates from my current location to a server, in very short frequences (30 seconds). Right now I am doing it with AlarmManager setRepeating(), but Android 5.0+ allows intervals from up to 1 minute to save battery.
My question would be:
Is my way really efficient? I need to run this repeating alarm in the background, even when the device is off. Currently I am executing pendingintent which leads to a broadcastreceiver which in turn executes to asynctasks, one for server authentication, one for posting the coordinates.
Is my way really efficient?
No. This is why it is not supported out of the box on Android 5.1+, and this is why Android 6.0 introduced Doze mode.
I need to run this repeating alarm in the background, even when the device is off.
I assume that by "the device is off" that you really mean "the screen is off". If the device is powered down, your code will not run.
You can use setExact() to "manually" do your own repeating events on a 30-second interval, where the work that you do on the alarm event includes scheduling the next alarm. This too will fail on Android 6.0+ devices, when the device is in Doze mode. The only way to get every-30-second behavior on Android 6.0+ would be for the user to add your app to the battery optimization whitelist in Settings.
Currently I am executing the pendingintent which leads to a broadcastreceiver which in turn executes to asynctasks, one for server authentification, one for posting the coordinates.
First, that is unlikely to be reliable. Your process can be terminated as soon as onReceive() returns, since Android thinks that your work is done.
Second, AsyncTask is usually pointless from a background operation like a broadcast. The entire point behind AsyncTask is for you to do work on the main application thread when background work ends, and there is little reason to do work on the main application thread in the background. For this and the above reason, use a single IntentService to replace the two AsyncTasks.
Third, there is a decent chance that, on any given alarm event, you will be unable to complete your work in 30 seconds, due to network congestion, server issues, etc. You need to make sure that you handle this appropriately, so that you do not wind up queueing up all sorts of requests (or, in your current implementation, fork lots of parallel AsyncTasks all trying to do their work).

detect when user sets alarm Android

I am trying to execute an action when the device detects that the alarm is set by the user.
Does anyone know how I can go about doing this in code? Would appreciate any advice.
Thanks
There isn't a unified "action" the user performs that will set the alarm. It would probably not even be correct to refer to it as "the alarm", because the alarm is just another application. The user can be using the stock alarm shipping with Android (in the Clock application, I would guess?), the phone manufacturer might have bundled its own alarm app with the phone, or the user might have downloaded a 3rd party alarm application from Android Market.
I would guess that the only thing all those different apps could have in common, is that they in some way could use the AlarmManager class to control when the alarm should go off. But I don't think this is something you can listen to (and it is also lots of other apps other than alarm apps that also use the AlarmManager).

How to Realize an Update Service is Running in the Background?

I'm currently working on an app for the Android OS that displays some data. To ensure this data is up-to-date, it is required that the app fetches updates from a remote server from time to time.
As the data does not change very often, this update should be carried out once per week. I want to give the user the ability to choose the weekday and daytime of the update (and optionally disable this feature completely).
The thing is: this update should be carried out even when the user is not using the phone at this moment, even when the phone is currently sleeping and even when the phone has been rebooted recently and the app hasn't been started yet.
The first thing I thought of was a remote service that starts at system boot, determines the time when to run the update, sets a timer and then waits/sleeps for the timer to fire.
Now, I was told I should rather use alarm timers or some kind of handlers... the more I read about this topic, the more ways to do this seem to exist.
Now, I'm a bit lost which method is the best for me... so here is what I need:
I want to execute some code at a time that is specified.
This timer is used to trigger the execution of code 7 days in the future. (i.e., every week at a given weekday and time)
The code should run WITHOUT waking the phone up if it is "sleeping" (screen dimmed).
When running the code, no activity should be started. i.e. no app pops up on the screen.
The code that is executed should fetch some data from the internet. If at this time no internet connection is available, the timer should be set to sleep 30 minutes and then try again.
After completing the code execution, the timer will be set for the next interval, which will be 7 days later.
The timer should be started at system boot, e.g., if I reboot the phone, the timer should determine the next date to execute the code and schedule the timer. This has to work without ANY user interaction! (i.e. without the app being started itself)
When "sleeping", the thread/service/timer/whatsoever should not consume any system resources if possible...
What i need is pretty much a simple unix cronjob.
I think anyone here knows "newsrob" for android? What I want to realize is pretty much the same as the newsrob-updateservice.
Android Service + Broadcast Receiver + Alarm Service will solve your purpose -
Your service will be invoked from BroadCast Receiver and In Broadcast receiver you should register for various events - BOOT_RECEIVER , ACTION_USER_PRESENT , which will take care of your ALARM reset and update task.
Thanks.

Categories