best way to build an android alarm clock - android

I am building an alarm clock app for android as my first go at an app. All my activities, views and a nice ui are ready.
My problem is that I just can't decide which is the best way to start each alarm.
As far as I can see, I have two options:
Option 1 - I can use android's built in AlarmManager to manage the alarm.
But if I do so, then how can I store the different alarm types?
What I mean is: there are four alarm types in my app: Standard, math, puzzle anf bar code scanner.
So, when the alarm goes off, I need it to know which dismiss activity to show.
My biggest worry is that I have been reading that some manufacturers disable the built in alarm manager such as HTC and a few others.
Option 2 - Store all the alarms in a database that my app will create, and have my app permanently running in the background.
But I feel this is excessive and wil use to many resources.
Can anyone Point me in the right direction? I want the app to work perfectly on every device.

Option 1 . I can use androids built in alarm manager to manage the alarm
This is the only sensible solution.
But if I do this then how can I store the different alarn types
Have an extra in the Intent in your PendingIntent for AlarmManager that indicates what specifically should be done when the alarm goes off.
My biggest worry is that I have been reading that some manufatures disable the built in alarm manager such as HTC and a frw others.
I am not aware of HTC messing with AlarmManager -- if you have a link to that, I'd love to see it. SONY, when the device is in STAMINA mode, will treat _WAKEUP alarms the same as their non-_WAKEUP counterparts. However, the user has to opt into STAMINA mode and can add apps to a whitelist to be left alone.
Store all the alarms in a database that my app will create and have my app permanently running in the background
Since your app cannot be permanently running in the background, this will not work well.

Related

How can I achieve the "wake up screen and go to Activity" behavior of the Android alarm clock app?

If I set a timer in the default Android clock app, then it has the following behavior when the time runs out:
If the phone is unlocked, a pop-up comes up allowing you to stop the alarm or navigate back to the app.
If the phone is locked (including if the screen is off), the screen turns on and you're taken to a special Activity to stop the alarm.
What is the best way to reproduce both of these behaviors in my own app? I've been copy-pasting various magical incantations involving AlarmManager for the second, but nothing is working. None of the questions that have come up when Googling things like "bring Activity to front" or "wake up phone" seem to be what I need.
This can't be done, and it's by design. There are 2 separate problems, and they're both impossible to implement.
Google has progressively disabled the ability to launch activities without user interaction in all recent API versions. They also disabled "springboard" behavior, where background services and/or receivers try to start activities from the background. You're supposed to use notifications to let the user know what you're trying to do, and when they interact with that notification, then your activity can be launched
There is absolutely no app, unless you have a custom ROM or a rooted phone, that can bypass the lock screen. It's a security issue, and the idea is the same as in the previous case -- you need to notify the user, and if they interact with the notification, they can be prompted to unlock their phone and your activity will launch

Alarmmanager and Jobscheduler are not reliable. How do apps provide notifications on time in Android 9 and 10?

This question has probably been asked a thousand times, but please don't mark this as duplicate. I have gone through every question/forum/advice/documentation available online on Alarm manager and Jobscheduler, but I still can't get them to work consistently on specified time. Either the answers on other questions are very old, not applicable anymore, or insufficient.
Use case: Provide notification of next meeting on Calendar; Reminder by user at X time to do something; At X time, get user's location to trigger a service (like home automation, when user is near home, XYZ happens).
What I have tried:
Jobscheduler: This works well, except when the app goes in Doze mode. For that I employed Alarm Manager.
AlarmManager: I have tried all the methods one by one, starting with setAlarmClock(), setExactAndAllowWhileIdle(), setExact(). They all work except when the device goes in doze mode.
Major issue is that most of them don't fire even within an hour of the set time (testing on Android Q / 10). This is so inaccurate, how are other apps functioning? For example apps like Calendar, Uber etc.?
I must be missing something.

How to set up reliable alarms while developing an application for Android Marshmallow or upper?

In the last year I have bought 4 different Android phones and all of them had got failures with the calendar notifications. Sometimes the notification for an event was shown and sometimes wasn't shown at all. I investigated a lot and found a problem in the "Calendar Storage" app, a Content Provider that is in the application framework layer. Notifications stop to be always shown as of the version 6.0.1. That's the reason why I want to develop my own calendar app that is not based on the Calendar Provider so that I can have reliable notifications again.
In the past, when we developed a reminder based app we used AlarmManager class and the method setExact() for make the application able to show a notification or anything else at a given date and time in the future. But in Marshmallow and upper it mustn't work because of the annoying doze mode.
They invented the method setExactAndAllowWhileIdle() in Marshmallow supposedly for setting alarms that can bypass the doze mode. But it says that it doesn't let you to put more than one alarm within 15 minutes. In a reminder based app that is a foolishness as the user MUST be able to put as many reminders as he wants and as close in the time line as he wants.
So my question is, how can I make my app to show reminder notifications at a given time in a way that always works regardless of the doze mode and so that I can have as many notifications as I want and as close in time as I want?
Thank you.
There's always the option of white listing your app so doze will still let it run the old way.
Another official way of making it work is sending an fcm notification, but you'd need a server to do the job.
Short from that I'm afraid the next official answer involves exact alarms.
If it's an option for you there is a chance you can start a service with STICKY. I'd expect the phone to never go into doze properly even if the service thread is blocked forever (it may help to assign it to a different process in the manifest so your ui never freezes). Putting a periodic old style Java timer to fire a callback at the right time may accomplish what you need.

AlarmManager does not work in the same way on every tested device

I'm currently developping an app, that need several alarm trigger at the exact time.
For this purpose i created a receiver that schedule the next alarm.
I also created a stress test :
alarmManager call my receiver which create an other alarm for the next minute.
At xx:30 and xx:00 i receive a mail with the log of all the alarm containing the expected date and the triggered date.
All work well on my phone, a samsung galaxy note 3 lite.
Sadly the behavior is not the same on all the devices.
Huawei Honor :I launched it yesterday at 6PM, it worked until 6:16PM and restarted at 6:45PM before completely stop at 7PM.
OnePlus 2 : Currently working, but some notification are not triggered, about 20%... that's a lot for what i need...
So i'm wondering, if some devices have an alarmManager that stop some Alarm When it abused of the system. Unfortunately, I did not find enough of Doc to support this theory.
Have you ever had this kind of problem, how do you solved it?
How can you make a system That will Delivered a notification at the right time with a success rate > 95%?
I had the same problem and after a lot of searching and workaround, I figured that Smart Manager of Samsung Devices in Lollipop and above may be the problem. This component can delay the Alarm Manager to goes off, it is triggered after 3 minutes, if the mobile is working on battery and with the screen closed. Of course you can deactivate the Smart Manager like this :
Launch Samsung Smart Manager application on the device
Tap Battery
Tap App optimization
Detail
Find Your APP
Select "Disabled for"
But in my case it didn't work, neither disabling the Smart Manager worked in 2 Samsung devices with Lollipop . What it did work was to "fool" Smart Manager by refactoring the name of my application's package to contains the String "alert" or "alarm", for example com.example.alarm.myApplication. You can also refer to this link for more information.
Also some other companies like Huawei have their own optimized app that can delay AlarmManager .
Also the new component Android Doze may delay the AlarmManager
Please refer to this link.
Finally your phone works well because is below Lollipop and does not contain Samsung's SmartManager.

Prevent that the app get stopped or paused by the OS

I'm developing and Android application on CodenameOne that needs to send a web request every 5 minutes even when minimized. How can I achieve this behavior in order to prevent that the request get stopped or paused by the OS?
You cant do that from the activity, you'll need to create background service.
http://developer.android.com/training/run-background-service/create-service.html
Use AlarmManager to set up your every-five-minute operation. Have it trigger a BroadcastReceiver, which in turn passes control to my WakefulIntentService (or your own IntentService that handles the WakeLock, that you will need so that the device stays awake while you do your work). Have the service do the "web request".
This is still not 100% guaranteed:
The user can Force Stop you from Settings, in which case your alarms are gone and nothing will happen until the user manually runs your app again
The user might block alarms from firing on certain devices, like various SONY Xperia models, that offer "stamina mode" or the equivalent
However, it is the best that you are going to get, short of rolling your own Android OS version.
The other guys answers are correct that you need to create a service but they somehow ignored the mention of Codename One.
Under Codename One you need to create a native directory for android and just place the service source code there (just use a blank service class that doesn't really do anything). Then you need to add to the build arguments the option android.xapplication where you would state the service XML attributes.
Having said that what you are trying to do is VERY wrong and you shouldn't do it in Android! You will drain the battery life from the device in no time and the service will be killed by the OS eventually (since it will be a battery drain). The solution is to send a push notification to the device to wake up the application.
In Android 9 and newer you can prevent your App falling asleep with a battery setting.
Long click on your App -> App info -> battery -> optimize battery consumption
Here add your App from the list.Hint: maybe the menu entries have a different name, depending on your phone.

Categories

Resources