0

I'm have created a kiosk application by following this guide in android developers - Lock Task Mode. The application can start itself automatically whenever the device finish booting up but the problem is whenever I restart/shutdown and start my device that has this restriction the device will not be able to boot it will stuck in the brand logo screen of the device and have to factory reset it to work again.

dpm.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);

This restriction is working fine if I don't restart the device but at some point the device needs to be shutdown. How can I set this restriction properly during start without breaking the deivce?

luhluh
  • 167
  • 1
  • 11

1 Answers1

0

I found a workaround to solve the problem. I have created a BroadcastReceiver for device shutdown and remove the restriction and re-enable the restriction when the device restarts.


public class ShutDownReceiver extends BroadcastReceiver {

    private static final String TAG = "ShutDownReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (Intent.ACTION_SHUTDOWN.equals(action)){

            DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            ComponentName cn = AdminReceiver.getComponentName(context);

            if (dpm != null && dpm.isDeviceOwnerApp(context.getPackageName())) {
                //This is a custom method
                setUserRestriction(dpm, cn, UserManager.DISALLOW_USB_FILE_TRANSFER, false);
            }
            Toast.makeText(context, "Shutting Down", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onReceive: ACTION_SHUTDOWN");
        }
    }
}

Add code in manifest

        <receiver android:name=".receiver.ShutDownReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
            </intent-filter>
        </receiver>

luhluh
  • 167
  • 1
  • 11