I am pretty new to android and I am currently developing an Android App that should run in KioskMode, so that a normal user can't exit the application or do anything outside of it.
What I already did:
- I set my app as a device owner
- I "screen pin" the app in the onCreate()-method in the MainActivity as a device owner
- I have a button in the MainActivity which later on allows an admin to exit by entering a password. Calls stopLockTask()
The Problem I encountered now is, that any user can exit the Screen Pinning by simply long pressing "back" and "multitasking" buttons simultaneously, because the tablet on which the app should run has hardwarebuttons which I can not simply deactivate. (at least I don't know how to do this without rooting the device)
So is there any way to deactivate this button combination for exiting screen pinning, or some neat workaround?
One approach I thought of was repinning the Application in my AdminReceiver class in the onLockTaskModeExiting(), but I´m still stuck on how to do this.
Here are some code snippets of my MainActivity and the AdminReceiver class:
AdminReceiver.java
public class AdminReceiver extends DeviceAdminReceiver{
@Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, context.getString(R.string.device_admin_enabled), Toast.LENGTH_SHORT).show();
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return context.getString(R.string.device_admin_warning);
}
@Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, context.getString(R.string.device_admin_disabled), Toast.LENGTH_SHORT).show();
}
@Override
public void onLockTaskModeEntering(Context context, Intent intent, String pkg) {
Toast.makeText(context, context.getString(R.string.kiosk_mode_enabled), Toast.LENGTH_SHORT).show();
}
@Override
public void onLockTaskModeExiting(Context context, Intent intent) {
Toast.makeText(context, context.getString(R.string.kiosk_mode_disabled), Toast.LENGTH_SHORT).show();
}
}
MainActivity.java
public class MainActivity extends Activity {
private DevicePolicyManager mDpm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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())) {
Toast.makeText(this, getString(R.string.device_owner), Toast.LENGTH_SHORT).show();
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
startLockTask();
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
stopLockTask();
}
});}
Any help is highly appreciated. Thanks!