John K
John K

Reputation: 371

How to disable back,home,multitask physical buttons?

Actually i'm developing an app and just this app should be opened on the device where is installed so i would be able to block / disable physical buttons for back,home,multitask.

I've read yet some articles about how to do it but still can't get how to implement it in my app.

The buttons i would disable are the following that you can see on the photo enter image description here

Upvotes: 2

Views: 3819

Answers (2)

akansha06 jain
akansha06 jain

Reputation: 46

//Just put below code onStart() method

@Override
    protected void onStart() {
        super.onStart();
        // start lock task mode if it's not already active
        ActivityManager am = (ActivityManager) getSystemService(
                Context.ACTIVITY_SERVICE);
        // ActivityManager.getLockTaskModeState api is not available in pre-M.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            if (!am.isInLockTaskMode()) {
                startLockTask();
            }
        } else {
            if (am.getLockTaskModeState() ==
                    ActivityManager.LOCK_TASK_MODE_NONE) {
                startLockTask();
            }
        }


    }

Upvotes: 3

Anand Jain
Anand Jain

Reputation: 2385

For back button you can Override onBackPressed method like below :-

@Override
public void onBackPressed() {
    // do what you want
}

For Home Button

@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

For Disable recent app button : -

Step 1

Add this permission to the manifest.xml file

<uses-permission android:name="android.permission.REORDER_TASKS" />

Step 2

Put this code in any Activity on which you want to block/disable the recent apps button

 @Override
protected void onPause() {
    super.onPause();

    ActivityManager activityManager = (ActivityManager) getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);

    activityManager.moveTaskToFront(getTaskId(), 0);
}

Upvotes: 3

Related Questions