-1

I want way to disable the Home Button & Back Button when a checkbox is checked in my application. I am on version 4.2.2. Here is my code, it does not work, the application stops when the box gets checked:

public void HardButtonOnClick(View v)
{
    boolean checked1 = ((CheckBox) v).isChecked();
    if(checked1)
    {   
        SQLiteDatabase db;                      
        db = openOrCreateDatabase("Saftey.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        ContentValues values = new ContentValues();
        values.put("hardBtn", "YES");
        db.update("Setting", values, "id = ?", new String[] { "1" });
        Toast.makeText(this, "Hard Button Locked", Toast.LENGTH_LONG).show();
        //SharedPreferences pref = getSharedPreferences("pref",0);
        //SharedPreferences.Editor edit = pref.edit();
        //edit.putString("hard","yes");
        //edit.commit();
        /* String Lock="yes" ;
         Bundle bundle = new Bundle();
         bundle.putString("key", Lock);
         Intent a = new Intent(Change_setting.this, ChildMode.class);
         a.putExtras(bundle);
         startActivity(a);*/
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        isLock = true;
    }
    else
    {
        SQLiteDatabase db;                      
        db = openOrCreateDatabase("Saftey.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        ContentValues values = new ContentValues();
        values.put("hardBtn", "NO");
        db.update("Setting", values, "id = ?", new String[] { "1" });
        //SharedPreferences pref = getSharedPreferences("pref",0);
        //SharedPreferences.Editor edit = pref.edit();
        //edit.putString("hard","no");
        //edit.commit();
        Toast.makeText(this, "Hard Button Un-Locked", Toast.LENGTH_LONG).show();
        isLock = false;
    }
}

How can I make this work? I do not want to hide the buttons, I only want them to not respond to clicks when the checkbox is checked.

Daniel
  • 2,355
  • 9
  • 23
  • 30
michael
  • 55
  • 1
  • 2
  • 7

4 Answers4

3

You can override the back key onBackPressed() and let it do nothing, but you can't override the home button but you can add some code to onStop() which is called upon home key press.

here is a piece of code to control backpress

@Override
public void onBackPressed()
{
    if ( checked1 ) // if checked do nothing , remeber tomove checked1 to be a class member
        return;
    super.onBackPressed();
}
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
1
@Override
public void onBackPressed() {
}

With this you override back button. Just do nothing in it.

This you can use if you do something special when u press back:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    int vfBackId = viewFlipper.getDisplayedChild();

    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

    // Do some stuff    

    }
    return false;
}

But you cannot override the home button. It's not provided from the Android system to handle the home button.

silvia_aut
  • 1,481
  • 6
  • 19
  • 33
1

you cant disable home button in your application but you can disable back button with override onBackPressed() and do nothing in that

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
1

You cannot disable home button.

There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit.

Home button is one sure short way to navigate to home screen.

If you want to handle the HOME button, implement a home screen.

Not able disable Home button on specific android devices

//Back key event detect

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    // Quit if back is pressed     
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Community
  • 1
  • 1
Hemantvc
  • 2,111
  • 3
  • 30
  • 42