user2625151
user2625151

Reputation: 49

disable home button in lollipop programmatically

I want to disable home and power buttons automatically after opening my application and enable them after hitting exit button in the application.

I have achieved this by making my application as the launcher application and it works until Jellybean (tested upto 4.1.2).

But the same app doesn't work around in KitKat and Lollipop versions. I figured that to use kiosk technique to disable home button in Lollipop. Refer to https://sdgsystems.com/blog/implementing-kiosk-mode-android-part-3-android-lollipop, it requires a device owner application and few steps to make it achieve.

Though my application is to automatically block the home button function, it doesn't work around.

My questions are:

If kiosk is the technique, then how to make it suitable for my requirement? (Looking for a guide through)

Upvotes: 0

Views: 2730

Answers (2)

hardwork
hardwork

Reputation: 781

First sorry for late answer. first thing is programmer really not blocking home button. They are use only trick to hide home button process behind the lock screen.

How can you do this.? simple. Just use your lock screen window as window manager screen that's why after pressing home your mobile screen will not minimize. This is little trick used in most success lock application.

and one more thing is how to overlap phone window top system status bar.

For this You can use code just like this and can modify as you want.

    manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));

localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;    

localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

//WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR |
  WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN |

       // this is to enable the notification to recieve touch events
       //WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
       // Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;


//localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 *      getResources().getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;

view = new customViewGroup(this);

manager.addView(view, localLayoutParams);   

and create customeVIewGroup class like this..

public class customViewGroup extends ViewGroup {

    public customViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.v("customViewGroup", "**********Intercepted");
        return true;
    }
}

Upvotes: 1

kash
kash

Reputation: 840

Here is the flow I suggest. Create a device owner app and take device ownership either through NFC bump or through adb. Add your package name to setLockTaskPackage() in the device owner app.

Now, in the app that you want to lock the device to, call startLockTask() method and your device will be locked to this specific application until you call the stopLockTask().

You can find the api information here.

On how to create a device owner app and guide you can refer to website of Florent Dupont.

Upvotes: 0

Related Questions