1

i'm creating an app, which will be used by kids and so client wants me to disable any way of getting out of it. I managed to block home button, also back button, but what about others app button? I can hide all down buttons, but it is possible to show them by swiping from down to top.

So is it possible to disable "other apps" button in Android, or somehow override it's action?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Adam Staszak
  • 1,674
  • 14
  • 16
  • 2
    how did you block home button? It is not allowed in android to block home button last time I checked. Post some code. – Illegal Argument Sep 15 '14 at 13:33
  • 1
    I think in order to make a child friendly app with this functionality, you will need either a custom rom for children installed on the device that will support this at a low level (like the one used in the nabi tablets) or to have root and be able to edit the configuration files for the rom manually. I feel like the app itself and the app that blocks navigation would need to be separate applications – Konner Rasmussen Sep 15 '14 at 13:37
  • Right, how Did You disable the home button ? Will be interesting to know, really. – icbytes Sep 15 '14 at 13:37
  • "Blocking" HOME button is simple, it only requires setting Your application as Launcher, so adding this code: in Manifest, in activity intent-filter. Than You need to choose app as a default launcher, in my case it is possible because we configure those tablets before giving it to users. – Adam Staszak Sep 15 '14 at 13:42

1 Answers1

0

Post ICS i.e. Android 4+, the overriding of the HomeButton has been removed for security reasons, to enable the user exit in case the application turns out to be a malware.


Here is article for blocking home button on devices below 4.0 - Overriding Home Button

Code Extract -

public class DisableHardButton extends Activity {
    TextView mTextView;
    ToggleButton mToggleButton;
    boolean isLock=false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTextView=(TextView) findViewById(R.id.tvInfo);
    mToggleButton=(ToggleButton) findViewById(R.id.btnLock);

 mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         isLock=isChecked;
         onAttachedToWindow();
     }
 });
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
        mTextView.setText("KEYCODE_HOME");
        return true;

    } else {
        return super.dispatchKeyEvent(event);
    }

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock) {
        mTextView.setText("KEYCODE_BACK");
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}

@Override
public void onAttachedToWindow() {  
    System.out.println("Onactivity attached :"+isLock);

if(isLock) {   
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
    super.onAttachedToWindow();

} else {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);     
    super.onAttachedToWindow();
}
}
}

XML -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hi! This is the testing of override home button" />

    <ToggleButton
        android:id="@+id/btnLock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="UnLocked"
        android:textOn="Locked" />

</LinearLayout>

Code for specifically blocking the back button -

@Override
public void onBackPressed() {
    //Do nothing
}

and for older then API 5 use this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //Do nothing
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
Confuse
  • 5,646
  • 7
  • 36
  • 58