Android Device Owner and SetLockTaskPackages

Gigex42Gigex42 USMember ✭✭✭✭
edited August 2017 in Xamarin.Forms


I want to create a kiosk mode for my android app which I can exit by entering a password.
For this I use dependency service to start the StopLockTask() and StartLockTask().

Because I dont know how to start these method within my dependecy service class I created a little workaround.

In my OnCreate() of my mainactivity I store the activity in some separeted static class.
Now I have access to the mainactivity from my ds.

Start and LockTask() does work when I press the buttons.

Now I want to use the method SetLockTaskPackages() using:

string packagename = "com.XX.XXX";
ComponentName componentName = new ComponentName(UnlockTaskInit.GetActivity(), Java.Lang.Class.FromType(typeof(AdminReceiver)));
DevicePolicyManager dpm = (DevicePolicyManager)Xamarin.Forms.Forms.Context.GetSystemService(Context.DevicePolicyService);

 public void Lock()
            if (dpm.IsDeviceOwnerApp(packagename) & dpm.IsAdminActive(componentName))
                dpm.SetLockTaskPackages(componentName, new[] { packagename });

I have set app as Device Admin:

public void SetDeviceAdmin()
                        Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
                        intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, componentName);
                        intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Device administrator");

I have set app as device owner:
adb shell dpm set-device-owner com.XX.XXX/.AdminReceiver

But when I start de SetLockTaskPackages method I get following error:
{Java.Lang.SecurityException: Admin ComponentInfo{com.XX.XXX/md53e824c34692992298f45eea02f037828.AdminReceiver} is neither the device owner or affiliated user's profile owner.

Even tough The IsDeviceOwnerApp and IsAdminActive method return true.



Sign In or Register to comment.