Is it possible in an acitvity override the callback called when the power button is pressed? My target is to prevent the screen off.
I found this - How to hook into the Power button in Android? -but it seems don't work.
THX
You can keep the screen on with a wake lock:
http://developer.android.com/reference/android/os/PowerManager.html
But you should be carefull with it because it really drains the battery.
Related
I hope this question wasn´t asked before, but I couldn´t find any solution for my problem. I have a widget which is also used for keyguard. I want to add an onClick handler for my widget. There is no problem, when the widget is on home-screen, the activity starts normally. But on keyguard the activity is launched, but isn´t shown, because the device is locked.
I don´t want to dismiss the keyguard with flags like WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD. I just want a little window to be shown, where I am asked to unlock the device, like it is opened when you click on several keyguard widgets, like for the android e-mail application.
Is there another flag to achieve this? Or do I have to do something with my intent, called to open the activity? Thanks for help.
For enabling or disabling lock screen in Android, we need to get the instance of keyBoard Manager, using getSystemService(). The syntax is as,
KeyguardManager keyguardManager =
(KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
For locking the screen use,
lock.reenableKeyguard();
and for disabling the lock use,
lock.disableKeyguard();
This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.
When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"
Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()
And of course we need to give permission android.permission.DISABLE_KEYGUARD
For my android app, I want to keep the screen from dimming while certain activities (e.g. CatActivity) are in the resume state. Does anyone know how to do that? is there a way to declare so in the manifest or in the activity itself?
UPDATE
I found the answer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Is there a way to turn it off? I don't see a removeFlags method.
To delete the flag simply call the clearFlags() method:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
I am a developing game using andengine. When I press the power button, the onPause() method is not being called. How do I fix the issue?
#Override
protected void onPause() {
//this method is not calling when press power button
super.onPause();
this.mEngine.onPause() ;
}
Weird, haven't heard of it not being called. Let me research a bit.
Okay, pressing power should always call onPause(), there seem to be some kind of bug in your code. Could you add some log calls to follow every step. and print the DDMS log.
Other possible problems:
It seems some devices misbehave and call onPause() twice when pressing the power button.
In this case try using the powerManager to see if the screen is actually turned of.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
//now do stuff
}
Maybe onPause() is called but onCreate() is called right after ?
When you turn off the device, the lock screen is displayed. This typically forces the display to a particular orientation. If the screen is not currently in that orientation, it will need to be changed, and the top activity's configuration appropriately changed to match.
You can add config changes like this:
android:configChanges="keyboardHidden|orientation" android:configChanges="keyboardHidden|screensize"
To make sure this doesn't happen.
I am logging each onCreate() and onDestroy() calls. I found out that once I click power button on my Android (and on the emulator too!) the phone calls in my activity
> onDestroy();
> onCreate();
Which kills my game and then immediately reopen it from the beginning. Of course once user unlock the screen the game is presented in main menu with all progress killed.
Is it possible to override or disable this behavior?
When you press the power button, the screen lock usually kicks in. This may trigger a configuration change on the activity currently in the foreground (the screen lock is usually in portrait mode), causing it to be destroyed and recreated.
Declaring android:configChanges="keyboardHidden|orientation" for such activities in AndroidManifest.xml prevents them to be destroyed and recreated but also implies that you will handle the configuration changes by yourself (if necessary) by overriding onConfigurationChanged
You can't override when the onCreate() and onDestroy() methods get called (at least not without experiencing extraordinary amounts of pain). The best thing for you to do is to figure out how to work within the confines of when they get called. Save your state in onDestroy(). Make it so that your app can tolerate this call sequence because quite frankly, it supposed/has to. That's just how android works.
I just developed a screen saver app and I found a strange behavior in its lifecycle.
My work flow is like this:
start my RegisterService, where I call registerReceiver method to register a BroadcastReceiver, which can receive ACTION_SCREEN_OFF.
2.In the onReceive method of this BroadcastReceiver, I start an activity as the screensaver.
3.In the activity, I write Log.i() statement to track its running.
My question is:
When the screen times out, or when I press the POWER key, the screen turns off, and the system will send ACTION_SCREEN_OFF message. As I expect, my receiver starts the screen saver activity. However, I find this Activity calls onCreate(), onResume(), onPause(), onResume() sequentially according to the output in logcat.
It seems as if some a activity comes at front of my screensaver and finishes immediately, so my screensaver calls onPause() and then onResume().
Any idea? This problem handicaps me in programming, please help. Thanks!
Well based on a brief study of the PowerManagerService.java source code, when it's time to turn the screen off, the system initiates an animation (look at line 2183 for the class source) to do that. That means that your activity will pause and then will resume after the animation has ended.
I cannot be 100% sure for this, since I haven't tested it in my environment but this is the only logical explanation I found for your situation.
Hope this helps...
I can recommend you something very easy that might work for you, if you do not want the pause behavior why don't you try to Override the method onPause() and just do nothing :P don't call super.onPause() and that will terminate the behavior of it.
Other thing that might work for you, declare a static variable, add 1 on the "onResume()" method and return to "0" when "onStop()" is called. now just evaluate when the "onResume()" is called and if the variable is "0" then is the first time, anything else do nothing.
I hope this helps cause there is no much information on your question to be more specific.