Make my app a device administrator

sepehrdavarnia.6336sepehrdavarnia.6336 USMember ✭✭
edited December 2014 in Xamarin.Android

Hi
I'm using these codes to male my application a device administrator, but its now working.

My activity:

protected override void OnCreate (Bundle bundle)
{
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) GetSystemService(Context.DevicePolicyService);
        ComponentName demoDeviceAdmin = new ComponentName (this,Java.Lang.Class.FromType(typeof(DeviceAdmin)));
        Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin );
        intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin,demoDeviceAdmin);
        intent.PutExtra(DevicePolicyManager.ExtraAddExplanation,"Device administrator");
        StartActivity(intent);
}

DeviceAdmin class:

[BroadcastReceiver (Permission = "android.permission.BIND_DEVICE_ADMIN")]
    [MetaData ("name", Value = "android.app.device_admin")]
    [IntentFilter (new []{"android.app.action.DEVICE_ADMIN_ENABLED", Intent.ActionMain})]
    public class DeviceAdmin : DeviceAdminReceiver
    {
        public override void OnEnabled (Context context, Intent intent)
        {
            base.OnEnabled(context, intent);
            Toast.MakeText(context, "DeviceAdmin",ToastLength.Short ).Show();
        }
        public override void OnDisabled(Context context, Intent intent) {
            base.OnDisabled(context, intent);
        }

        public override void OnPasswordChanged(Context context, Intent intent) {
            base.OnPasswordChanged(context, intent);
        }

        public override void OnPasswordFailed(Context context, Intent intent) {
            base.OnPasswordFailed(context, intent);
        }
        public override void OnPasswordSucceeded(Context context, Intent intent) {
            base.OnPasswordSucceeded(context, intent);
        }
    }

Posts

  • Solved it

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

    device_admin_sample.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>
    
  • alinjojobaalinjojoba INMember ✭✭

    Thanks for sharing the solution!

  • PeresPeres BRMember

    Hi,

    I am trying to do that, to make my app to be in kiosk mode.

    When it start, show a message "Screen is pinned this keeps it in view until you unpin... "

    How can i close this dialog on start?

    Thanks

  • VahidMazloomiVahidMazloomi USMember ✭✭

    Mr. Sepehr Davarnia a lot of thanks for sharing your code and solution.

    It works fine.

  • waseemalamerywaseemalamery USMember

    a lot of thank Mr.sepehrdavarnia.6336

  • Gigex42Gigex42 USMember ✭✭✭✭

    @Peres said:
    Hi,

    I am trying to do that, to make my app to be in kiosk mode.

    When it start, show a message "Screen is pinned this keeps it in view until you unpin... "

    How can i close this dialog on start?

    Thanks

    Im stuck at this point too.

    Found out you can use SetLockTaskPackages() to not show this message. But the problem is you have to be the device owner.

    Already posted about this but no solution
    https://forums.xamarin.com/discussion/101341/android-device-owner-and-setlocktaskpackages#latest

  • saranbsaranb USMember

    while executing the above code i got this error

    Severity Code Description Project File Line Suppression State
    Error: No resource found that matches the given name (at 'resource' with value '@xml/device_admin_sample'). dictionary C:\Users\saran\documents\visual studio 2017\Projects\dictionary\dictionary\obj\Debug\android\manifest\AndroidManifest.xml 10

    i stuck on this part help me!!

  • @saranb said:
    while executing the above code i got this error

    Severity Code Description Project File Line Suppression State
    Error: No resource found that matches the given name (at 'resource' with value '@xml/device_admin_sample'). dictionary C:\Users\saran\documents\visual studio 2017\Projects\dictionary\dictionary\obj\Debug\android\manifest\AndroidManifest.xml 10

    i stuck on this part help me!!

    Select device_admin_sample.xml, go to properties, set "Build Action" to AndroidResource.

  • saranbsaranb USMember
    Thanks for your solution @mirceamocian
  • prabhu1182prabhu1182 Member ✭✭

    I need administrator mode app for disable camera, disable Bluetooth, disable WiFi and Memory card also. Can any one help code.

    Thanks in advance.

  • prabhu1182prabhu1182 Member ✭✭

    Hi,

    I am getting the error "Invalid start tag device-admin"

  • diockmandiockman Member ✭✭
    Can your share the source code, please
Sign In or Register to comment.