Android Single-Use Devices (COSU) fails to enter lockTaskMode after the reboot

493 Views Asked by At

I'm trying to create a single-use devices (kiosk app) and follow tutorial below: https://codelabs.developers.google.com/codelabs/cosu/index.html?index=..%2F..%2Findex#0

The code works very normal, except that "reboot" case. Every after reboot, it fails to lock the app, keep fall into "LOCK_TASK_EXITING" every after rebooting.

07-05 15:45:14.583   785   802 V ActivityManager: Broadcast: Intent { act=android.app.action.LOCK_TASK_ENTERING flg=0x10 cmp=com.google.codelabs.cosu/.DeviceAdminReceiver (has extras) } ordered=false userid=0 callerApp=ProcessRecord{6438256 785:system/1000}
07-05 15:45:14.584   785   802 V ActivityManager: Broadcast: Intent { act=android.app.action.LOCK_TASK_EXITING flg=0x10 cmp=com.google.codelabs.cosu/.DeviceAdminReceiver } ordered=false userid=0 callerApp=ProcessRecord{6438256 785:system/1000}
2

There are 2 best solutions below

7
Martin Zeitler On

see GitHub ...one can work around the issue alike:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (mSharedPreferences.getBoolean(KEY_PREF_RECREATED, false)) {

        mSharedPreferences.edit().putBoolean(KEY_PREF_RECREATED, false).apply();

        // start LOCK TASK here

    } else {

        mSharedPreferences.edit().putBoolean(KEY_PREF_RECREATED, true).apply();

        finish(); // close the app
        startActivity(new Intent(this, MainActivity.class)); // reopen the app
    }

    setContentView(R.layout.activity_main);
}
0
Rodrigo Quelhas On

From: https://developer.android.com/work/dpc/dedicated-devices/lock-task-mode#kotlin

To call the Activity.startLockTask() method, the activity must be running in the foreground, it is suggested calling it inside the onResume() method of an Activity or Fragment.

Example:

// In our Fragment subclass.
override fun onResume() {
    super.onResume()
    // First, confirm that this package is allowlisted to run in lock task mode.
    if (dpm.isLockTaskPermitted(context.packageName)) {
        activity.startLockTask()
    } else {
        // Because the package isn't allowlisted, calling startLockTask() here
        // would put the activity into screen pinning mode.
    }
}