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:
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!
If the activity is set as DEFAULT
HOME
<activity
android:name=".AppActivity"
android:label="Locked Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
every time the user press the home button the activity can re-enable the lock on onCreate
, onResume
or onStart
. If the app was forcefully unpinned using overview and back key and the recent activity list is empty, the user can only press the home button which will re-enable the lock.
As the question is 12 months old, I'm too still looking for a more effective solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With