After achieving device ownership, I am trying to implement a method to instruct the device to lock any given app into kiosk mode (or screen pinning mode). Since I have devic
I've not enough reputation for a comment, just would point out that for devices with physical buttons (like the Samsung Galaxy Tab A mentioned by @chairman) one way for manage the forced unpinning of your application is to implement in your DeviceAdminReceiver class the following:
@Override public void onLockTaskModeExiting(Context context, Intent intent)
So if your user want to for the unpin you can always re-pinning your app ;)
The setLockTaskPackages()
is used the specify which applications (through their package names) will be able to programmatically be pinned without user confirmation.
The setLockTaskPackages()
is called from your device owner app (most probably in your DeviceAdminReceiver
's onEnabled()
method).
So, in you owner device app, you'll have something like :
mDPM.setLockTaskPackages("com.foo.myapp");
and then, in your "com.foo.myapp" application, you will be autorized to call :
startLockTask();
Your application will immediately enter the Pinning mode, without any user confirmation.
If you don't first register
your application with setLockTaskPackages
, the application will be pinned but the user will have to confirm first.
Also notice that when an app is registered
with setLockTaskPackages()
, it has some different behaviours than the manual pin:
stopLockTask()
;Here's a code snippet that should get you going:
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(this, DeviceAdminSample.class);
if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
// Device owner
String[] packages = {this.getPackageName()};
myDevicePolicyManager.setLockTaskPackages(mDeviceAdminSample, packages);
} else {
// Not a device owner - prompt user or show error
}
if (myDevicePolicyManager.isLockTaskPermitted(this.getPackageName())) {
// Lock allowed
startLockTask();
} else {
// Lock not allowed - show error or something useful here
}