I have written an alarm program which works nicely in most regards. When the alarm rings the user is presented with three options to select:
Stop
Snooze
Reschedule
The user then selects their choice, the alarm stops and what happens next depends on which option they selected.
But now I have a problem. Say the alarm goes off and the user accidentally presses the home button whilst fishing their phone out of their pocket. Now the alarm is still ringing, but the app is no longer in display! I don't know how to avoid this scenario. Any ideas?
If you use activity as dialog, and if home button is pressed causing activity to close, you can simply put alarm's stop method in onstop() method of activity.
Related
I was wondering if there is any dedicated way of setting application "timeout". Let's say that user minimizes my app - I want to close it if he don't resume it for like 20 mins. How to do it?
"user minimizes my app" means that user click "home" button, so the app is "onStop" right?
I think that when the App went to the background (onStop), you can register "AlarmManager".
And the AlarmManager will destroy your app after 20 min.
And you need to unregister the AlarmManager if the user came back to the app.
I have a service that runs to get user current location through LocationListener. I stop the service on the activity's onDestroy() but this method is only called when the user hits the back button to exit the app.
I want the service to stop both when the user hits the back button to exit and when they hit the home button. However, as far as i know, there's no way to intercept the home button. How do people deal with this issue?
Is it normal to leave the location listener running after the user hits the home button?
Use onPause() instead on onDestroy()
I have read other posts that suggest that I use a service but I am not sure that it will do what I need it to. I am trying to create an alarm app but once the user presses the home button, the alarm will never go off. Is there a way to keep my app running when the home button is pressed?
You should implement Service running in background (read this manual) or use an AlarmManager as suggested by Greg (that is better solution)
I have an weird situation regarding an alarm app. I have an app that is "awake" the all night and at certain point the alarm starts and is the user press the back button or a button in the User Interface (UI) the alarm sound stops and move to the next activity (until here everything is Ok!)
The scenario is: The screen is deemed, the alarm starts so the activity is launched and the user press the power button. After it press the power button again (the activity starts again) and press Home button.
Pre-Lollipop
The app is running in background and the alarm is still ringing(normal behavior)
Lollipop
The screen becomes black, the user can't do anything on his phone until he reboots and the alarm is ringing
Did you have experienced this before? I see there are lots of black screen issues with lollipop but I only see "solutions" from user point of view and I would like to make my app avoiding this issue.
This issue was related with the keyguard, I was disabling the keyguard when the intent of the alarm was received because I wanted to disable the lock screen, but I realized the best place to do that is in the method onResume.
So I move the disable keyguard to onResume and enable keyguard to onPause and that made the trick
I am writing a GPS based application for Android. I want a button to exit the application which will turn GPS off but I do not want back arrorw or home to turn the processes off.
I have read This thread and many others that do a great job of explaining why I don't want to do what I want to do. The issue is that there are background processes and GPS is left running if I just back arrow out of the application. I understand that I can turn these off with OnStop() or OnPause() but I don't want back arrow or home to stop anything. I want all the GPS and handlers to continue running no matter what the user does unless he hits EXIT on my main screen which takes him to another screen that is to confirm the exit and do the cleanup. That way the user can back arrow out of the exit screen without actually exiting if it was hit by mistake.
Please understand that this application will be used in bright sunlight in adverse conditions with gloves on. It is very difficult to read anything so I use high contrast fonts 100 high. It cannot stop unless the user really wants it to stop. But just letting it run will kill the battery. I have no problem with the behavior of the system if I just let it run, just with the idea that the battery will run down and I don't want any simple button push, which may be accidental, to stop the services. I want it just as hard to kill this apps background processes as it is to turn the phone off, but I want to be able to do it.
I have tried finish(), which is just a back arrow, and every other method I have seen over the last three hours of Googling. I have thought of changing my EXIT button to a Turn GPS off button but then if the user uses back arrow to exit he gets to the main page and that turns GPS back on.
Just to be clear. My app has a menu based main screen. Any useful page will return to that menu if back arrow is hit. One of the menu items is EXIT. Hitting that menu item takes you to an exit screen that has a button that says EXIT. If the user got here by accident, he can back arrow back to the menu. If he hits the EXIT button on the exit page, I can turn off all the handlers and GPS and go to the phone or tablet home screen. At least that is what I want and can't figure out how to do.
I have used similar apps that use OnStop() or OnPause() to turn off the GPS and they are a pain. That is not what I want. Please don't tell me that is what I want to do. I know how to do that. That is not what I am asking.
Thanks.
UPDATE:
Thanks to Chris I have ALMOST solved this. I can turn my handlers and GPS off with my EXIT button and then run this code
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
UPDATE #2
The problem that I just found in further testing is that the application goes back to the EXIT screen when it is called for a second time.
UPDATE#3
Adding finish(); after startActivity(startMain) seems to work. The finish does a back arrow to the main menu. Hopefully once I get all my code installed it will still work. I removed the third line above so what I now have is this:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
finish();
There is a really simple way to resolve this. Your main problem is that you need the EXIT activity to not go back to the MAIN activity (ie: After the user EXITS, the activity stack should be empty) if the user wants to exit.
Have the MAIN activity call the EXIT activity using startActivityForResult(). The EXIT activity can then return RESULT_OK if the user really wants to exit, and it will returnRESULT_CANCELED if the user used the BACK button to go back.
The MAIN activity then gets the result in its onActivityResult() and can call finish() if the user really wanted to exit, or just do nothing if the user cancelled the exit.
This will result in the activity stack being completely cleared if the user wanted to exit, so that the next time the user starts the app, it will go to the MAIN activity.
I have thought of changing my EXIT button to a Turn GPS off button but then if the user uses back arrow to exit he gets to the main page and that turns GPS back on.
That is the root of a more proper solution than trying to "exit" the process - you just need to handle the accidental re-activation.
you could automatically send a HOME intent after disabling the GPS.
you could override the back button there, and send a HOME intent instead (if the GPS is off).
you could set a flag in shared preferences when you turn off the GPS to indicate that it is supposed to be off, then check that in any Activity where you would otherwise turn it on automatically. If you detect that the GPS is supposed to be off, instead of your usually choices just show a dialog that the GPS is off and ask if they want to turn it back on, or "quit" (ie Intent to the home screen).