I'm developing a simple app for Android using ActionScript 3.0 in Flash CS5 and I'd like to know if there's a way to map the physical back button of the Android's phone to tell my animation to go to the first frame.
I've red that post: Disabling the phone's back button (AIR for Android /ActionScript 3)
so maybe it is possible ? If yes HOW.
Thank you !
//first register key down listener
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
//then listen to the back key
private function handleKeys(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK ) {
logDebug("=>handleKeys.");
NativeApplication.nativeApplication.exit();
}
}
Have you tried overriding onKeyUp method?
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do your stuff
}
return false;
}
Related
sorry for any spelling mistakes as english is not my first language.
So, to the question:
I am making an android app using jquery mobile and Apache Cordova, both in the latest versions, so the question is:
Is there a way i can disable the back button on the android phone when in the app?
Add this to your MainActivity:
#Override
public void onBackPressed() {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// appView.loadUrl("javascript: clickBack()");
return true;
}
return super.onKeyDown(keyCode, event);
}
you can handle backbutton like this
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
so its not going to close whatever you are going to do i think
I am working on android application using PhoneGap. I need to handle Device back button functionality by using the below code:
import com.phonegap.DroidGap;
public Class MyClass extends DroidGap {
appView.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
finish();
return true;
}
return onKeyDown(keyCode, event);
}
});
}
By using the above code, Application getting exited because i have used finish(); But i want nothing should be happened on click of Device back button. How can i acheive that? Please help me.
Why do you need to do this at the Java level? You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
}
I'm working on an input device that has a button that I'd like to act both as a home button when pressed by itself, but also work as a modifier for the other buttons on the device.
I know you can do this in the kcm file with other buttons, and have something like:
key ESCAPE {
base: fallback BACK
alt, meta: fallback HOME
ctrl: fallback MENU
}
Is it possible to have something like:
key CTRL_LEFT {
base: fallback HOME
{SOMETHING HERE}: CTRL_LEFT
}
so that I can press that button with another button and have it not fallback on HOME?
Thanks in advance! :D
This functionality is not possible on a system level: see
http://source.android.com/tech/input/key-character-map-files.html for a list of all options possible.
However, if you would like to have it on an application level, you could program it in as follows:
import android.view.KeyEvent;
private boolean home = false;
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (event.getScanCode() == 29) //29 == CTRL_LEFT
home = true; //if we release this button, HOME needs to be invoked
else home = false; //so CTRL_LEFT was used as a modifier: no need to invoke HOME
//allow the system to pass key handling to the next listener
return super.onKeyUp(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (event.getScanCode() == 29 && home == true)
{
super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME));
return true; //so we absorb the event
}
return super.onKeyUp(keyCode, event);
}
Then, as long as this application is set up to receive the keystrokes (usually a privacy issue unless you're coding it in for yourself), it can process the keystrokes and dispatch the HOME button when need be.
Should you decide to proceed thus, you'd have to remove the
base: fallback HOME
from your code.
I want to block hardware back button in android ,in order to prevent from going back to other activity..
Thanks in advance...
Here is code that allows you to handle the back key in an activity correctly on all versions of the platform:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( Integer.valueOf(android.os.Build.VERSION.SDK) < 7 //Instead use android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
// Take care of calling this method on earlier versions of
// the platform where it doesn't exist.
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the
// platform.
return;
}
sources:http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
If the 'other activity' is yours, you can set it to not appear in the history list.
Otherwise, remember that the phone belongs to the user and not to you, and stop trying to tell them what they can and can't do with THEIR device.
I want to pop up the software keyboard when the user presses the search hardware search key.
At the moment I use the following function with doesn't seem to work for the search key but which does work for the back key:
The logging doesn't get even tiggered through the search key.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
Log.v(TAG, "On back key press");
renderPrevious();
return false;
}
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Log.v(TAG, "On search key press");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
return true;
}
I have no text field but want to handle the input directly myself if that matters.
this method setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) doesnt wakeup the keyboard. Check this link. More to know about it check this example. hope it helps
Edit:
try this showSoftInput method
Maybe this would be helpful:
InputMethodManager inputMgr = (InputMethodManager)getSystemService Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
http://plainoldstan.blogspot.com/2010/09/android-set-focus-and-show-soft.html
"When experimenting I was not actually getting what I wanted until I realized I should have an emulator device with no hardware keyboard:"