简体   繁体   中英

Android: Stop Back Button From Exiting LockTask / Kiosk Mode

I want to implement a KioskMode , I'm targeting only Android L , since this is a very specific App.

I already went through the process of setting my App as DeviceAdmin, and DevicePolicyManager.isLockTaskPermitted(this.getPackageName()) already returns true .

I then start a LockTask via startLockTask() .

Everything is fine, but when I hold down the backbutton , the app still exits the kiosk mode.

I have overridden onKeyPress to show a custom Dialog for unlocking the app, but this does not hinder android to automatically exit my lock task if the user holds down back.

I don't really know what to do at the moment and would be thankful for every input.

I now have overridden

@Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
    if(KeyCode == KeyEvent.KEYCODE_BACK)
    {
        BackDownButtonPressed = true;
        if(VolDownPressed)
            showTaskLockDialog();
        return true;
    }
    else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
    {
        VolDownPressed = true;
        if(BackDownButtonPressed)
            showTaskLockDialog();
        return true;
    }
    return  super.onKeyDown(KeyCode, event);
}

@Override
public boolean onKeyUp(int KeyCode, KeyEvent event) {
    if(KeyCode == KeyEvent.KEYCODE_BACK)
    {
        BackDownButtonPressed = false;
        return true;
    }
    else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
    {
        VolDownPressed = false;
        return true;
    }
    return super.onKeyUp(KeyCode, event);
}

@Override
public void onBackPressed()
{
    return;
}

@Override
public boolean onNavigateUp() {
    return true;
}

@Override
public boolean dispatchKeyEvent (KeyEvent event)
{
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return true;
}

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //do something or nothing in your case
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

For the record, I am using a Samsung SM-T700 Tablet with Cyanogenmod CM12.1

Perhaps you need to override onKeyLongPress

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //do something or nothing in your case
        return true
    }
    return super.onKeyLongPress(keyCode, event);
}

Not sure if it's helpful at all, but I wrote a blog about setting Kiosk Mode here: http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/

And also wrote sample code for it here: https://github.com/sureshjoshi/android-kiosk-example

Not sure if you see any major differences between your code and mine, but I just tried to do a long press on a Samsung Galaxy Tab 4 running Android 5.0, and it won't exit the app.

Could it be something with rooting with Cyanogen?

If you don't have this in your code, perhaps add it in and check out if you see any problems:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Remove title bar and notification bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
        mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        if (!mDpm.isAdminActive(deviceAdmin)) {
            Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
        }

        if (mDpm.isDeviceOwnerApp(getPackageName())) {
            mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
        } else {
            Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
        }

        mDecorView = getWindow().getDecorView();
}

and

protected void enableKioskMode(boolean enabled) {
        try {
            if (enabled) {
                if (mDpm.isLockTaskPermitted(this.getPackageName())) {
                    startLockTask();
                    mIsKioskEnabled = true;
                } else {
                    Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
                }
            } else {
                stopLockTask();
                mIsKioskEnabled = false;
            }
        } catch (Exception e) {
            // TODO: Log and handle appropriately
        }
    }

Just to close this topic..

I couldn't figure out a perfect solution to this day. My current workaround is receiving an event if the user leaves the kiosk mode and just entering the kiosk mode again.

Sadly this leaves the user with 2 toasts saying "screen unpinned" and "screen pinned", which is unfortunate. But this satisfies my current needs.

I have the same problem: How to stop "holding the back button" from escaping "Lock Task mode" on Android 6+

The issue only occurs on my Android 7 tablet.

Running the app on my Android 6 tablet fixed the problem.

Could you add some code to illustrate your current work-around?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM