2

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

CodeChimp
  • 4,745
  • 6
  • 45
  • 61
John K
  • 371
  • 5
  • 26
  • show us your code,where are you stuck? –  Jun 06 '18 at 12:22
  • 1
    Actually i was following this [guide](https://hackernoon.com/disable-android-home-back-and-active-apps-buttons-f7450dfeddcd) and i can't get how to implement onKeyDown method – John K Jun 06 '18 at 12:31

2 Answers2

3

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);
}
Anand Jain
  • 2,365
  • 7
  • 40
  • 66
  • 2
    Canno't resolve method TYPE_KEYGUARD while with TYPE_KEYGUARD_DIALOG the app crash with the following error: Window type can not be changed after the window is added. – John K Jun 06 '18 at 12:38
3

//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();
            }
        }


    }
  • 1
    That's a great solution but it's give me a lot of annoying Toast's and also when i open the app is Saying "this screen is blocked etc..." can i disable all this stuff? – John K Jun 06 '18 at 12:53
  • when startLockTask() called, system will ask user the permission for entering pinning mode. – akansha06 jain Jun 06 '18 at 12:59
  • yeah i've noticed it but i'm looking for something that doesn't permit to the user to decide if block it or not :l but anyway if i don't find anything other ill use this method – John K Jun 06 '18 at 13:01
  • 1
    @akansha06jain While answering questions is great, it would benefit future users if you explain what your code did. Add some relevant information about the code. – Abhi Jun 06 '18 at 13:02
  • Do you need a dedicated device? If yes you should give a look to this guide https://developer.android.com/work/cosu#java . Using a Device policy controller and whitelisting your package, if you call startLocktask() the "annoying" popup is not shown – lukaspp Oct 04 '18 at 08:45