I want to display deals from website when the screen is locked in an android application.
I succeeded to display the activity when the screen is locked. But while pressing the home key of the device the activity exits. So I want to know exactly how we detect the home button on the device is pressed or not.
You can save the state of your Activity on the SharedPreferences so in your activities onStop method you can set the variable "running" to false and then on your onResume method set it to true.
Related
I have a single activity wear application with singleInstance set as launchMode.
When I swipe right or put the palm over the watch, the application goes to the background (pause/stop), then when I tap on its icon it opens again (resume) but it shows a new main activity and not the original one.
I would like the app to only have 1 main activity.
I did unsuccessfully try other values for the launchMode and I also unsuccessfully tried to set flags.
I do not post the code since it is just simple wear app with a TextView and a Button on the main activity. When the button is pressed a counter increases and updates the text. When the app resumes the counter shows zero as if the button was never pressed.
You lose your data since the old activity gets destroyed when going to the background and gets restored when coming back. This is the normal behavior.
When you want to keep your data, you need to save and restore it e.g. save in onSaveInstanceState() and restore it in onCreate().
My Android application(Application A) launches another application (say Application B) upon click of a button.
Now I want to implement "auto start" functionality wherein Application B will be launched as soon as Application A is launched.
For this, I created a checkbox and used SharedPreferences to store the value of checkbox.
Then, in my onCreate() method of Activity A, I am checking the value of checkbox from SharedPreferences and launching Application B in case the value is "true".
The Problem:
The problem I am facing is, when the user exits "Application B" (and comes back to Application A) the onCreate() of Application A is getting called again and Application B opens up again. This sets off a infinite loop and at exit of Application B, user returns to Application A and goes to Application B again.
I know onCreate() gets called multiple times (when we change orientation, keyboard opened, Activity goes into background and is killed by system), but is there any clean way of doing this?
To reiterate, my requirement is to launch Application B from Application A if the "auto start" checkbox is checked in Application A.
My suggestion will be to use the method onPause of the activity in application A and set a flag there "application B was called". Then if this flag is set do not call application B in onCreate of the activity in Application A and unset the flag.
If application B is too long in the foreground application A might be suspended by the system and the flag will be reset. In such case maybe it is good idea to have the flag stored in some persistent storage (e.g. SharedPreferences).
EDIT One more thing: the flag should be set in onPause only if the activity is being paused, because the other application will be shown (this will be easily determinable, because everything happens in the same class).
I want it like:
Start an emergency activity on, say long press MENU button. Though there is a phone lock.
As this is an emergency, on long press MENU button, it will capture the key Event/press and will start the emergency activity defined by the developer.
but now the problem is,
when there is screen Lock, key-guard will be lock i.e it cant capture any key Event/Press in a phone lock state.
Is there any way to solve this problem ?
U can't dispatch even when screen is lock but using accelero meter ie by shake u can open screen lock and do what ever u t ...even u start any thing in background
I need show PIN Code activity after that (we have App with many Activities):
User press hardware button "Home" and go back to App.
List item App Screen go to sleep mode then go back.
What ways do I need to use?
capture home key event(KeyEvent.KEYCODE_HOME) and sleep event(Intent.ACTION_SCREEN_OFF) ,use a variable to record it.
when go back to app activity judge the vaviable and show your PIN code activity.
I want to disable the lock screen in a certain app. My problem is that this also disables the lock screen for the whole phone. So I added lock.reenableKeyguard(); to the onPause() method, but this is called not only when the user exits the app but everytime a new activity starts in that app. Do I have to disable the lock screen in every activity and add this onPause method? Is there way to manage this only in the first activity that is shows when the app starts?
Btw I also reenable the lock screen in the onStop and onDestroy methods. The other problem with onPause is that it is also called when I turn off the phone's screen (by pressing the power button or what is that called), so when I first open the app, I turn off the screen then turn it on, there is no lock screen, but when I turn it off-on again, there is it, as I reenable it in the onPause method.
After playing with this a couple of hours I figured out I forget to implement onResume():
public void onResume() {
lock.disableKeyguard();
super.onResume();
}
This way every time I open the activity/turn on the screen, the lock screen is disabled.