I'd like to detect when a user presses any hardware button on a sleeping android phone (or at least the home and the power button).
Until now I have used a BroadcastReceiver for the ACTION_SCREEN_ON event, which works great but it is also raised when e.g. you get a message, a phone call or when an alarm starts.
ACTION_USER_PRESENT is also no option because this is only raised when the user really is present an knows the unlock code, so he isn't tampering with the phone.
Is there a way to handle that?
no its not possible
but this can be used for other purpose
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)
{
}
return true;
}
Related
My app has a long running background service. I registered my service with a broadcastreceiver which handles SCREEN_ON and SCREEN_OFF actions. However, the problem with using this approach is that while it does work, whenever the phone was woken up by incoming phone calls, power cable plugin events or alarmmanager etc, SCREEN_ON action also triggers and I don't want that.
Is it possible to "physically" capture the power button click events when only my background service is running? I'm not trying to override power button.
The existing answers don't completely answer the question and leave out enough details that they won't work without more investigation. I'll share what I've learned solving this.
First you need to add the following permission to your manifest file:
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
To handle short and long presses add the following overrides to your activity class:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
event.startTracking(); // Needed to track long presses
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyLongPress(keyCode, event);
}
Note: It is worth noting that onKeyDown() will fire multiple times before onKeyLongPress does so you may want to trigger on onKeyUp() instead or other logic to prevent acting upon a series of onKeyDown() calls when the user is really holding it down.
I think this next part is for Cyanogenmod only. If the PREVENT_POWER_KEY constant is undefined then you should not need it.
To start intercepting the power key you need to set the following flag from your activity:
getWindow().addFlags(WindowManager.LayoutParams.PREVENT_POWER_KEY);
To stop intercepting the power key (allowing standard functionality):
getWindow().clearFlags(WindowManager.LayoutParams.PREVENT_POWER_KEY);
You can switch back and forth between the two modes repeatedly in your program if you wish.
reference :
https://stackoverflow.com/a/10365166/2724418
Can Android Wearable apps detect the power button as a KeyEvent or by other means? I would like to run a listener service to detect the wearable power button.
I tried this but no log corresponding to a KeyEvent occurred.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.e(">>>>>>", "FindPhoneActivity event "+event);
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
Log.e(">>>>>>", "FindPhoneActivity onKeyDown");
//event.startTracking(); // Needed to track long presses
return true;
}
return super.onKeyDown(keyCode, event);
}
I have also experimented with two solutions used on normal android phone apps
Registering a Broadcast Receiever
Creating an onPause event with PowerManagement
Both methods described here.
Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
I tested the above with logs attached to the event but neither produced logs.
I am developing an Android apps where user is able to find the phone once it is lost and I plan to implement the remote ringing function, I would like to do something like when the alarm is ringing, no matter the volume up or down key is pressed, the volume will still remain loudest while the alarm is ringing... But I have no idea how to achieve this... I just able to detect the event of volume keys pressed... Kindly need help for this.. Thanks...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
Toast.makeText(this, "Volume Up", Toast.LENGTH_LONG).show();
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
Toast.makeText(this, "Volume Down", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
Your code works fine for me. I threw it into a basic app, and it successfully intercepted the key presses without changing the volume on my device.
Have you tried overriding onKeyUp as well as onKeyDown?
EDIT: Note that this does not work in Services. You simply cannot intercept hardware volume button presses in services.
Alright as I have been asking the last couple days and inching closer and closer to the final outcome of this question:
Is it possible to use the volume (up / down) buttons to start an app?
Here is the code I am working with:
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("myApp", "LONG PRESS");
}
//my code here
return super.onKeyLongPress(keyCode, event);
}
I am wondering what I am doing wrong. Nothing in the code is giving me errors, its just not running when I tell it to.
Note:
I am testing this on a live android if that helps out at all.
Any advice would be wonderful.
Is it possible to use the volume (up / down) buttons to start an app?
No, sorry. You cannot use hardware buttons to start apps, with the exception of the CAMERA button (where it exists) or the MEDIA button (where it exists, typically on headsets). For those, you would register a BroadcastReceiver in the manifest for their respective broadcasts, and bear in mind that those broadcasts are only sent out if the foreground activity does not consume the key event (e.g., music player pausing when the MEDIA button is pressed).
I need catch long press of volume button when phone is sleeping(screen off) and I know this code:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
// to your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
is not helpful(it works only in active intent
I'm curious about this too.
While this isn't necessarily an answer, I have done some research on Services (to catch the volume press I'm guessing) and BroadcastReceivers (onReceive() would receive an intent for ACTION_SCREEN_OFF and probably set a flag for the screen being off). My thinking is starting the service when the screen turns off and kill it when the screen turns on, but I don't know how to join onKeyLongPress and the service.
The sequence I see happening is this: Screen turns off -> BroadcastReceiver receives this command and starts the service to watch for volume press -> receive volume press and do your logic -> kill the service if the screen turns on.
I found a site that apparently handles screen off/on and has an example for an activity and a service but I can't get something together yet:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
One thing I'm worried about when using a service is battery life and processing time but that will be testing down the road. I will hopefully be able to mitigate that with killing the service when the screen turns on. I'll try to keep this entry updated as I make progress.
Good luck!