I am new to android. I was going through the manual, and read following:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted
Can the alarm be saved in the app. If yes Can you please tell me how.
Can you please provide me with the code.
This should get you going.
In short, you relieve your app from keeping a watch on the alarm itself.
Instead you tell the system to keep track of it for you, and just create a class that should be awoken by the system once the alarm goes off.
http://www.vogella.com/articles/AndroidServices/article.html#scheduleservice_scheduling
Related
some context:
I have an alarm app I use for myself that locks the screen when the alarm goes off for the duration you request prior. Essentially your phone is a ringing brick for x minutes. The only problem is my sleepy self is very irrational and in the morning I figured out that if I turn off the phone and get to the app location and uninstall it before the receiver gets called (boot completed) then I can bypass it bricking my phone. This didn't use to be a big deal when my LG G2 was on 4.2.2 because of how fast the receiver was called I would usually have to restart my phone about 5-8 times to uninstall the app before it was bricked so I just gave up and quit trying. Now, I upgraded to 4.4.2 and the receiver is called about a full 10 seconds later letting me delete the app on the first try every time. Making the app completely useless.
What I have tried:
I have tried using quick_boot in the manifest but I believe that this is only for HTC because on_boot doesn't get called for that OS for some reason. I have also tried the user present which only seems to work after the boot is completed when doing things like unlocking your phone.
Is there really no way to make onReceive be called quicker than onBoot? It would make sense if there isn't , I'm just hoping someone could provide a definite answer either way.
In some cases it is. I'm not 100% sure but i think (some) systemapps have higher priority then the ACTION_BOOT_COMPLETED event. AND there is ACTION_SCREEN_ON which should be triggered before ACTION_BOOT_COMPLETED.
I should have answered this a week or so after I asked this question because I found a pretty useful workaround, although, it is a little sloppy.
I made a new activity(homeLock) with the intent filter . homeLock extends activity and my old main activity(alarmMain) extends homeLock now instead of activity. All homeLock does is start the overlay service(so you can't stop the alarm/use the phone) that will be turned off by alarmMain when it determines whether an alarm should be ringing currently or not. In alarmMain there is a button now that says "change home" which lets you make homeLock the home application. Now, when you turn off the phone and restart to try and delete the application before onBootReceived is called which starts the broadcast receiver(triggers alarm and overlay) the homeLock activity is called which puts an overlay on the screen until it can be removed after the application determines if an alarm should be playing or not (after onBootReceived).
Basically, before you go to sleep just set this application as your home application from within the app or through settings. Now, it should be impossible to delete the application or turn off the alarm once it has started ringing until it has rung its duration because there will always be an overlay on the screen even when restarting the phone.
Obviously this addition is only needed for phones that boot slowly or extremely degenerate sleepers, or both like me. While it is unlikely this will help anyone because it is such a unique problem I thought I should post the workaround I have been using just in case someone does end up finding it useful.
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.
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.
I have the following scheme in my application.
Broadcast receiver listen for an BOOT_COMPLETED action and sets repeating alarm.
Alarm starts a service via PendingIntent.
Service checks for some data by internet, and when new data are available, shows notification, which is runs an activity, when user select it.
All is working perfectly except one thing. When i close the app from Task Manager on my device, process is killed and my alarm no more works. So the process is stopped until next restart of device.
Setting android:process to different for the service and activity does not help. Debugger shows me that we have two different processes, but closing an app from the task manager kills both processes.
I have created two different applications, one just an activity, and the second one for broadcast receiver and service.
In that case all is working as i need. But now i have another issue. Two .apk files. I have tried to find a solution to merge two apk's in one for Market, but looks like its impossible. Ask user for installing two apk for one does not good idea i think.
So my question is how i can solve this?
When i close the app from Task Manager on my device, process is killed and my alarm no more works. So the process is stopped until next restart of device.
This is a good thing. The user, by killing or force-stopping your application, is saying "I do not want you to run again". Developers should respect their users wishes.
So my question is how i can solve this?
Treat your users with respect and allow them to force-stop your app if they choose. You can re-enable your alarms on the next manual run of your application.
You can also detect if they did this, by having your AlarmManager-triggered code keep track of when it runs -- if, the next time the user manually launches your activity, you determine that the alarm code has not run in far too long, that indicates the user force-stopped you. You might use this information to suggest that the user go to your PreferenceActivity and change how your alarms behave (e.g., run every 24 hours instead of every 10 minutes), so that the user does not feel the need to force-stop your application.
I have a service that will be started every once in a while via an AlarmManager.
When it starts I want to be able to check if the device is currently being used or if it is idle.
I could simply check if the screen is on, but it is possible that if the device was charging, or on a dock, that the screen could be on but not being used. So that is is not a perfect method.
Is there a solid way determine that the device is idle?
If not, I figure if the device is plugged in, then in my case I could just carry on as if the device was idle. However, I haven't been able to find a way to check if the phone is plugged in beyond capturing a Broadcast Intent. Is there a way to just check without having to capture the broadcast intents continually?
Android has an in built application called Autolock that can be customized.Please refer these links.
http://www.androidtapp.com/autolock/
http://developer.android.com/guide/practices/design/responsiveness.html
Hope this will help you.