How to stop “holding the back button” from escapin

2019-04-11 16:05发布

问题:

I'm attempting to use "Lock Task mode", as described on https://developer.android.com/work/cosu.html

My goal is to have a single app running in a "kiosk mode", so that the user cannot leave the app.

I'm using Google's "Test DPC" app from Google Play as the DPC, which I have made the owner of the device via: adb shell dpm set-device-owner com.afwsamples.testdpc/.DeviceAdminReceiver.

It all works really well at keeping a trivial app (which calls startLockTask()) in kiosk mode. (I have added this trivial app to Test DPCs list of apps which are allowed to use "Lock Task mode".)

The problem is that if a user holds the back button, they escape "Lock Task mode" while displaying a confusing toast of "App is pinned: unpinning is not allowed on this device".

Is there a way of stopping this escape mechanism?


Update: The problem occurs on an Asus P00A with Android 7, but does not occur on an Android P00C with Android 6.

回答1:

I have a similar issue if the user clicks the home button (that can destroy the previous activity), next back button

A trick is to desactivate the back button

@Override
public void onBackPressed() {
    // do nothing
}