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.
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
Hi I am looking for disabling the Only HomeKey In Android. WHat i am trying to do is
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
//action
}
if (event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
//action
}
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
//action
}
return false;
}
But by this my back button is too getting disabled. Is there any way to just disable the HomeKey in android. Please reply.
The documentation states:
Home key. This key is handled by the framework and is never delivered
to applications.
More specifically, the key is consumed by the PhoneWindowManager in the method interceptKeyBeforeDispatching(..). In this method the key is handled and consumed, meaning that Android does not allow overriding this functionality.
UPDATE:
The reason why your back-behavior does not work anymore is because you yourself have handled it. The key enters the onKeyDown-method and you can consume the key (yes or no) by returning true or false. By implementing this:
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
You explicitly state that you have handled the Back key in your Activity which also means that the default behavior of "going-back" is overridden. To restore to its original behavior use return super.onKeyDown(keyCode, event);
Yes, it is possible. In you manifest, under your activity deceleration, replace
<category android:name="android.intent.category.LAUNCHER" />
with
<category android:name="android.intent.category.HOME" />
Note that with this change, your application will not be visible in the "All Apps" section. Also, user needs to set you application as the default home screen. Try out "Toddler Lock" application, it handles this scenario nicely.
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;
}
I've noticed that the 'back' request used to be made at the point a user pressed down on the 'back' key but at some point this was changed so that a 'back' request is made instead at the point the 'back' key is released. (Correct me if I am wrong!) Does anyone know from which SDK (or API Level) exactly this change was made effective? I think it was SDK 2.0 (API Level 5) and hence have the code in my Activity as follows but would like to be certain...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR)
handleBackRequest();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR)
handleBackRequest();
return true;
}
return super.onKeyUp(keyCode, event);
}
Use onBackPressed() for Android 2.0 and higher. Use onKeyDown() for Android 1.6.
With the code in my previous post I was getting some unwanted behaviour (when dialogs, the android keyboard etc were showing up) and on CommonsWare's advice, changed my code to the following and it seems to be working alright for the different SDKs...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK
&& android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR)
{
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed()
{
// handle back request here
}
... Let me know please if there's anything here not quite right!
I am sincerely trying to display alert box onClick of Home key, but I am not succeeded. So please help me to write the code otherwise, show me the code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return false;
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
it is not possible to intercept KEYCODE_HOME by external applications:
http://code.google.com/p/android/issues/detail?id=1979
I don't think you can intercept HOME key presses. It is reserved to ensure that you don't get locked within any application.
KEYCODE_HOME : This key is handled by the frame work and is never delivered to applications.
So you can't use it