Android Alarm Manager plus broadcasting confusion - android

Hello i am relatively new to android so sorry for any mis-interpretations/confusions.
I am working on an application which triggers alarm on the selected time, it working fine, but there is a thing which makes me confuse.
When user selects alarm time, alarm is set for that time but if user changes the time of their android mobile phone, the alarm will be triggered on the time ie- (SystemClock.getTimeInMillis() + selectedTimeInMillis).
How to avoid this situation, i want to trigger the alarm on the exact selected time even if user changes the time of the mobile.
Any help is appreciated.

You could add a request to an internet time server like this:
How to get current time from internet in android
So it will always use the real time and not the system time. I don't think there is another way because you never know if the user changed the system time.

Related

Periodically run a service in the background

We are designing an app that users use on a subscription basis. When the user pays for a particular period, a receipt is issued from our server valid for the duration. The app is designed to be used "offline", so it will only be connecting with the internet to get the receipt from the server.
When in "offline" mode, the app waits for the paid duration #-of days until it requires the user to pay again. We are having problems implementing this.
We first tried to use an always running background service. But after reading https://stackoverflow.com/a/2682130/5753416, http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/, we decided to use AlarmManger to schedule periodic alarms to check for the payment.
We are now facing problems with the alarm when the user changes the time/date. We are listening to Intent.ACTION_TIME_CHANGED and Intent.ACTION_DATE_CHANGED broadcast, but that isn't being broadcast when setting the date to the past.
My question is what is the right way of implementing a similar functionality. Any advice is appreciated.
edit
issues with the broadcast
ASOP bug issue
There is one solution what you need to do is check phone up time so you can schedule alarm for some time like may be an hour or depending on your freq. So what you need to do is check phone uptime + duration for which the user is paid if that passes then cancel his subs. The only extra thing u need to check is if phone restarts once net should be connected to access your service.

Handling app updates (AlarmManager)

I am developing an app that connects to and modifies data in a database by executing php files.
If I need to make changes to the database or php files, this may cause old versions of the app to behave unexpectedly and crash. For this reason, I want to force users to update the app when such changes are made.
Right now, I have a method that connects to the database and compares the apps version to the databases version. This works fine but I call it every time I access the database (very often) which significantly slows down the usage of the app. Is there a better way to do this? I have read that I could use an AlarmManager or BroadcastReceiver to check for updates every X amount of hours. But what if the user closes and doesn't use the app for a few days. Will these timers get called as soon as the user starts the app and thus be able to force an update?
The Android AlarmManager is an API that let you communicate and program alarm with the Android Alarm Service. Think of it as similar to a Linux Cron job. As soon as the alarm is programmed, then it'll be triggered even if your app isn't running, because the alarm is triggered by the alarm service and not by your app. For instance, the only thing you need to do is to program your alarm. It's important to note that when you restart your device then your alarms are cleared, so you need to reprogram then in every reboot. You can do this by capturing the BOOT_COMPLETED broadcast, so you can reprogram your alarm every time the device boots up. Check out the definition of the Android AlarmManager. A common pattern to do what you want is to program an alarm that sends a broadcast or starts a service, then in that service you can query your server. You need to consider that when the device is sleeping then the alarms couldn't be sent, so you need to work with wakelocks. This class will help you with that, check it out.

Alarm when device hasn't been touched by user in x hours?

Is there a way to get an alarm to fire some time after the user has last touched the device? i.e. when the device has been in the users pocket for a while, or when the user is sleeping.
I need to do a db-locking update for 2 minutes every few days, preferably when the user doesn't want to use the app.
Edit:
I have no clue when the user will not be using the app, since it's something that will be used anytime during the day, just before going to bed and just after waking up.
Currently I'm just picking a random time between 07:00 and 08:00 every 3 days (using AlarmManager, restarting on boot, etc), hoping that the user doesn't really need this feature at that time of day, but I'm pretty sure someone will.

Android - Service, Boot and check time

I have an android app, and I want that the user can decide when (day,hour,and minute) to do something.
So, I have to:
1) start a service when the android phone boots and when the user runs the app (for example after the installation)
2) check (how? how many often?) if the current time is the time choosed by the user
3) run a method if the time is right.
I searched a lot, and I'm a bit confusing...
I found that I have to use a BroadcastReceiver to check when the phone boots,then start an IntentService to have a background process that's check if the time is a "choosen time" and then call the method.
Am I right?
But how do I check the time? How many often should I do that?
You're right. You can use a BroadcastReceiver to check for boot. As for the chosen time, I recommend that you use the AlarmManager class to have your Service run at a particular time or interval.
ref: http://developer.android.com/reference/android/app/AlarmManager.html

Count-down timer/alarm API for android

I'm learning Android and want to write some kind of count-down timer application, so I get a ring-tone after certain minutes are elapsed. The timer also should work if the user has closed the application or switched to another one. If my application is running, it should be able to display the remaining time.
I've tried with CountDownTimer, but this seems only to work when the phone is activated, but not like the alarms which could ring you up at the morning. What other similar API alternatives are there to activate the device if the time is elapsed?
You can use AlarmManager for this purpose.
count-down and alarm are two very different things (even thou both count time).
Count-down you probably want to run a service and put a notification with the flag ongoing = true updating the value of the time.
Alarm you want to use the AlarmManager (as pointed out by #PgmFreek) that you can schedule a specific time that the system will call an Intent for you.

Categories

Resources