AlarmManager effect on battery for an Alarm clock app - java

I want to built a alarm clock app for android
I'm using an AlarmManager that starts a service every 1 seconds to check system time.
now I have 3 question:
1- Is there an other way to check system time every 1 second? and what is the best way for this work?
2-what is this my way on battery charge? is it cause to reduce battery charge very faster?
3- what mechanism use the android alarm app for checking time?
please help me to built this app
thanks

Why don't you just use the AlarmManager functionality supplied by the OS, and not try and invent this yourself. Basically this allows you to make a call to a service at a predetermined time (probably ultimately what you want to do).
What you suggest would be very problematic for battery, and doesn't sound like a good approach.

Related

Using AlarmManager for periodic task Android

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.

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).

How to call a function continuously in background in an Android app?

I know it might seem a repeated question, but I want to call a method to report geolocation position every minute as long as the application is open. Other answers recommended using CountDownTimer class. Is there a better way in Android to repeatedly call a function in background?
No idea who suggested a count down timer, but in Android you can use Services namely the Background Service. See here
You could consider something like an AlarmManager - which will run periodically, i.e. every minute to execute an Intent
The risk you run, if something is continuously running, is it will be shut down by the OS.
Alternatively, if you're checking network connectivity etc then you can look into BroadCast receivers - and there's no need to run continuously in the background.
It depends on how you are getting the location. If you use play services you can take advantage of the LocationRequest class. For more information about how to use play services to get the location, please have a look at this training put together by Google.
Hope this helps.

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).

Android: Update some weather Data from server every X hours

What is the best and most efficient way in android to schedule an update for some weather data from the server every X hours?
Thanks
Sam
If you are asking about scheduling system, a good way is using AlarmManager, which can wake up your activity even if it is not running.
Here is some info: http://developer.android.com/reference/android/app/AlarmManager.html
And here a nice example with Notification: http://android.arnodenhond.com/tutorials/alarm-notification
Register your app with AlarmManager.

Categories