How Do You Call OnVolumeDown In Other Functions? - java

I have:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// Do your thing
TheTimer.cancel();
TheTimer.start();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
Right now I have a countDownTimer and I want to call onKeyDown() within it. I know that I want
onKeyDown(KEYCODE_VOLUME_DOWN, ?????)
What is it I put into the second argument?

Move your code in onKeyDown() into a another fuction. And it's not ideal to manually call fuctions like onKeyDown as they called automatically by the system when that events happens.
void keyDown() {
TheTimer.cancel();
TheTimer.start();
}
Then in the onKeyDown() and inside the countDownTimer call the keyDown() method.

Related

How to handle different keyboard action in Anrdoid app?

I have attached a physical keyboard to an Android tablet using USB. How can I call a function or perform some action in an Android app on pressing a paticular key, for example when pressing the zero key?
How could I implement the below code (for the zero key)?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
// your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
It was such a silly mistake i didnt import Keyevent ..

Handling the Back Button

I am working on the following code:
private class HandleBackButton implements OnKeyListener
{
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg1==KeyEvent.KEYCODE_BACK)
{
showResults(0);
}
return true;
}
}
I am somewhat new to android and my purpose is to operate the above code when the back button is clicked. User can click the back button any time. But, how can I set this listener to the Activity? I can't find something like this.setOnKeyListener().
I am using Android 2.3.3.
For the Activity you should override onBackPressed which is invoked when you press the back button. OnKeyListener dispatches key events to the view. You find setOnKeyListener defined in the View class
Interface definition for a callback to be invoked when a hardware key
event is dispatched to this view. The callback will be invoked before
the key event is given to the view. This is only useful for hardware
keyboards; a software input method has no obligation to trigger this
listener.
Just override the onKeyDown() method of Activity.
You don't have to set a listener then.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showResults(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
Optionally you can also override onBackPressed() if your api level is >= 5.
You can use onBackPressed():
#Override
public void onBackPressed() {
showResults(0);
}

Button return = action button home

I'm doing an Android app and I would know if this was possible:
When the user press the return button, it does the same action as the home button.
Following the question I linked: Override back button to act like home button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Should be the solution.
This overrides the backbutton, and puts the task to the back. Which emulates hiding like the home button. Read the other question for more details, and solutions.

Stop Sound when Hold key is pressed

I want to stop media sound when the user press hold key or power key
I do this
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENDCALL || keyCode == KeyEvent.KEYCODE_POWER){
pauseSound();
}
return super.onKeyDown(keyCode, event);
}
But this override function didn't invoke when Power or end call is pressed
Any idea?
Thanks it solved, Ovveride EndCall & Power is forbidden for security reasons, I overrided "OnPause" (of activity) and it works good, Thanks again

Android : startActivityForResult() with BACK button functionality

I would like to start a new activity for a result, with startActvityForResult(), but I would like to have the back button working as normal in the new activity.
Currently when I invoke a new Activity for result, nothing happens when I press the back button in the new Activity.
I tried something like this:
#Override
public void onBackPressed() {
setResult(0);
super.onBackPressed();
finish();
}
in the new Activity, but it didn't work. Still nothing happens when the back button is pressed.
Is there a way around this?
EDIT : I could of course load the last Activity in the onBackPressed() (can I?), but it seems like a rather crappy hack.
Alex Ady's answer solves my problem, but I still don't understand why onBackPressed() doesn't work. The working code now is something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
setResult(1);
finish();
}
return super.onKeyDown(keyCode, event);
}
I could use an explanation.
You could try
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
You shouldn't have to override the Back button behavior at all. By default, if the user presses the back button, the result will be Activity.RESULT_CANCELED.
Try getting rid of the line that contains the finish().

Categories