I'd like to redefine the "volume" button on an android phone. For example,When I press the increase or decrease, the volume will not be change, but only to print a word.
Just override the OnKeyDown method like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
//Do whatever you want to do on Volume Up
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
//Do whatever you want to do on Volume Down
return true
}
return false;
}
PROTIP: If you want this behaviour on all your activities and not only one, just do this in MainActivity.java (or whatever you want to call it) and make every other Activity extend MainActivity.
PROTIP 2: Don't do this unless it is absolutely necessary and you notify the user it works like that. Android users usually complain about it not having a common behaviour between apps.
You have to override dispatchKeyEvent() method like this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
Toast.makeText(this,"Your First Word",Toast.LENGTH_SHORT).show();
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
Toast.makeText(this,"Your Second Word",Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
Related
I am developing an app for Android Wear and are using the hardware buttons.
I can manage to catch the buttons with the onKeyDown override:
public boolean onKeyDown(int keyCode, KeyEvent event){
Log.i("CLICK", Integer.toString(keyCode));
if (event.getRepeatCount() == 0) {
if (keyCode == KeyEvent.KEYCODE_STEM_1) {
// Do stuff
return true;
} else if (keyCode == KeyEvent.KEYCODE_STEM_2) {
// Do stuff
return true;
} else if (keyCode == KeyEvent.KEYCODE_STEM_3) {
// Do stuff
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I would also like to catch a click on the middle (rotary) button,
but when I click this button, the watch is going back to the default 'home'(watchface)-screen,
and no event is being logged.
I can manage to catch the rotary scrolling, but it's the click I'd like to use.
#Override
public boolean onGenericMotionEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
// // Note that we negate the delta value here in order to get the right scroll direction.
// float delta = -RotaryEncoder.getRotaryAxisValue(ev)
// * RotaryEncoder.getScaledScrollFactor(getContext());
// scrollBy(0, Math.round(delta));
return true;
}
return super.onGenericMotionEvent(ev);
}
Is this possible, or is it impossible to override the functionallity of the middle watch button?
AFAIU It should be getting KEYCODE_HOME as key event on pressing RSB in onKeyDown().
If it is the case then it is controlled by Android framework only and apps can't do anything with it.
Here is the official description
Key code constant: Home key. This key is handled by the framework and
is never delivered to applications.
https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME
I use below code to go back in webview at first try. But for the low render ability, I used XWalkView replace the WebView.
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mWebView = (WebView) findViewById(R.id.webview);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
if (MainActivity.mTencent.isSessionValid()) {
MainActivity.logout();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When change to XWalkView, I only find this about go back in the XWalkView. But I cannot find an example about to use it.
When I not implement the back button envent, the app will exit after I double click the back button.
My question is:
1. How to use go back in the XWalkView, if some code may be more helpful.
2. How can I disable the back button click event, when I not use the go back functon.
Thank you in advance.
After days of digging, I solved this: put this in the activity xwalkview in. Though this works, but the go back sometimes lost some history. So I also want someone give a better answer here.
for goback:
public boolean onKeyDown(int keyCode, KeyEvent event) {
//WebView mWebView = (WebView) findViewById(R.id.webview);
XWalkView mXWalkView = (XWalkView) findViewById(R.id.xWalkView);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mXWalkView.getNavigationHistory().canGoBack()) {
mXWalkView.getNavigationHistory().navigate(XWalkNavigationHistory.Direction.BACKWARD, 1) ;
} else {
finish();
if (MainActivity.mTencent.isSessionValid()) {
MainActivity.logout();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
for disable back event you can override either of these method dispatchKeyEvent, onBackPressed, onKeyDown. Refer to this answer for more.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.dispatchKeyEvent(event);
}
You can use xwalkviewhistory.cangoback()
I'm wondering which KeyEvent action is called when a user presses the little upside-down triangle in nexus phone when soft keyboard is open.
In normal mode Nexus looks like this and the normal code works fine:
Nexus without keyboard
But when keyboard pops up it looks like this and the code won't work:
Nexus with keyboard
For android API up to 5:
#Override
public void onBackPressed() {
// your code.
}
For android before API 5 you must use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Please refer to How to handle back button in activity
EDIT:
This methods works only if the keyboard is hidden..
according with this answer: Detect back key press - When keyboard is open
The best action to be implement is dispatchKeyEventPreIme.
An example is:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
mActivity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
Where mActivity is your activity class (this).
While doing digits programming, i am using following method :
Digits.authenticate(authCallback,R.style.CustomDigitsTheme1);
which directs me to digits authentication screen(without showing my xml design file).
Now, when i press back button, it shows me my xml with digits auth button as below.
which i do not want.I tried conventional ways of disabling back buttons but they did not work. Is there any way i can disable back button on authentication???
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
This may helps you.
Try Using these
#Override
public void onBackPressed() {
// your code.
}
and for older then API 5 use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
I need to create a service that would be running in the background, displaying an icon in the notification bar, listen to the volume buttons being presses and prevent them from changing the volume, so that annoying beep won't be heard.
What do you think, is that possible? And if yes, where should I start?
Thanks!
edit:
found this snippet that works great for an activity, now will have to try it with a service.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
Toast.makeText(this, "UP", 1500).show();
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
Toast.makeText(this, "DOWN", 1500).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
edit2:
found a nice tutorial over here http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/
created a service, but the above code doesn't seem to fit into it... hmm...
What do you think, is that possible?
No. You cannot listen for volume button presses in a service.