Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Help Using Device Policy Manager

Good morning, I need the help of someone here in StackOverflow to create my kiosk mode app.

I tried to find the solution to create the kiosk mode app but nothing worked.

I need to develop this app in Visual Studio with Xamarin.

My requirement is to hide the home bar. I tried with this:

[Activity(Label = "app", MainLauncher = true, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, Theme = "@android:style/Theme.Holo.Light.NoActionBar.Fullscreen")]

This hides the bar, but when someone drags the bar to the top it reappears and the user can close it.

I found a solution for Java but I can't convert to Xamarin.

Please can you help me?

Regards, Andrea

Update:

I tried this:

[BroadcastReceiver(Permission = "android.permission.BIND_DEVICE_ADMIN")]
[MetaData("android.app.device_admin", Resource = "@xml/device_admin")]
[IntentFilter(new[] { "android.app.action.DEVICE_ADMIN_ENABLED", Intent.ActionMain })]
public class DeviceAdmin : DeviceAdminReceiver
{

}

I tried to launch this command via adb but it gime me this error:

C:\Users\user\AppData\Local\Android\sdk\platform-tools>adb shell dpm set-device-owner app.app/.MainActivity
usage: dpm [subcommand] [options]
usage: dpm set-active-admin [ --user <USER_ID> ] <COMPONENT>
usage: dpm set-device-owner <COMPONENT>
usage: dpm set-profile-owner [ --user <USER_ID> ] <COMPONENT>

dpm set-active-admin: Sets the given component as active admin for an existing user.

dpm set-device-owner: Sets the given component as active admin, and its package as device owner.

dpm set-profile-owner: Sets the given component as active admin and profile  owner for an existing user.

Error: Unknown admin: ComponentInfo{Tabdealer.Tabdealer/Tabdealer.Tabdealer.MainActivity}

My activity is:

public class MainActivity : Activity
{
    [BroadcastReceiver(Permission = "android.permission.BIND_DEVICE_ADMIN")]
    [MetaData("android.app.device_admin", Resource = "@xml/device_admin")]
    [IntentFilter(new[] { "android.app.action.DEVICE_ADMIN_ENABLED", Intent.ActionMain })]
    public class DeviceAdmin : DeviceAdminReceiver
    { 

    }

deviceadmin.xml:

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
  </uses-policies>
</device-admin>

But I don't know how to use DevicePolicyManager.

like image 992
andrea meneguzzo Avatar asked Dec 11 '25 21:12

andrea meneguzzo


1 Answers

I've only been at it for a couple days but I think I can help you.

I'm assuming you registered your device admin receiver in code and did not share it. If not you need to do something like this.

DevicePolicyManager devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
ComponentName deviceAdminComponent = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdmin)));
Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, deviceAdminComponent);
intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Device administrator");
StartActivity(intent);

I think you defined this command incorrectly.

adb shell dpm set-device-owner app.app/.MainActivity

After adb shell dpm set-device-owner it should be PackageName/.DeviceAdminReceiverName

If you right-click the android project and look under the Android Manifest tab it should tell you the Package Name. If yours says app.app then you got the first part correct. While your poking around make sure your device_admin.xml is marked as an Android Resource in visual studio.

Debug your deviceAdminComponent to make sure your using the full name. Yours will be different but it will look something like this.

enter image description here

Then include the full name of your DeviceAdmin in the adb shell command.

adb shell dpm set-device-owner app.app/md50db01e1d47836fa6db48b854d43dd279.DeviceAdmin

Now next time you restart the application all administrative features will be available. Such as;

 devicePolicyManager.SetLockTaskPackages(deviceAdminComponent, new[] { PackageName });
 StartLockTask();

At last you can hide the status bar with flags. That doesn't require any administrative privileges though.

Window.AddFlags(WindowManagerFlags.Fullscreen);
Window.ClearFlags(WindowManagerFlags.ForceNotFullscreen);
like image 163
clamchoda Avatar answered Dec 14 '25 09:12

clamchoda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!