Home > Enterprise >  How to display device admin permissions interface to user in Android Xamarin C#?
How to display device admin permissions interface to user in Android Xamarin C#?

Time:06-07

I have an Android Xamarin framework app and I need to lock the device when the focus of an activity is lost. I have DevicePolicyManager object and a DeviceAdmin component in my class. I need to request for device admin permisions so that I override onwindowfocuschange to use the DevicePolicyManager object to lock the device. How can I request for the Device Admin user interface to show up in my oncreate method. I have extended the DeviceAdminReceiver class and registered all the meta data and receivers in the class below

 [BroadcastReceiver(Permission = "android.permission.BIND_DEVICE_ADMIN",Name="LockScreenAPI.AdminReceiver")]
    [MetaData("android.app.device_admin", Resource = "@xml/device_admin_sample")]
    [IntentFilter(new[] { "android.app.action.DEVICE_ADMIN_ENABLED", Intent.ActionMain})]
    public class AdminReceiver : DeviceAdminReceiver
    {
        public override void OnEnabled(Context context, Intent intent)
        {
            base.OnEnabled(context, intent);
            Toast.MakeText(context, "Device Admin Enabled", ToastLength.Short).Show();
          
        }

        public override void OnLockTaskModeEntering(Context context, Intent intent, string pkg)
        {
          Toast.MakeText(context,"'Device entering locked mode",ToastLength.Long).Show();
        }

        public override void OnLockTaskModeExiting(Context context, Intent intent)
        {
          
        }

        public override void OnDisabled(Context context, Intent intent)
        {
         Toast.MakeText(context,"Admin mode disabled",ToastLength.Long).Show();
        }
    }

and then the declared the DevicePolicyManager and deviceAdmin components in the main activity like below

 [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity,Preference.IOnPreferenceChangeListener
    {
   

        private LockReceiver _lockReceiver;

        //declare a devicepolicymanager object
        private DevicePolicyManager _devicePolicyManager;
        //declare a component name
        private ComponentName _deviceAdmin;
        private Preference preference;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            //request device admin permissions from the user
            permissions();
            _devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
            _deviceAdmin = new ComponentName(this, Java.Lang.Class.FromType(typeof(AdminReceiver)));
            //check if admin mode is enabled
            if (_devicePolicyManager.IsAdminActive(_deviceAdmin))
            {
                Toast.MakeText(this,"Device admin is enabled",ToastLength.Long).Show();
            }
            else
            {
                //show the device permissions
                try
                {
                    Intent devicePolicy = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
                    devicePolicy.PutExtra(DevicePolicyManager.ExtraDeviceAdmin,savedInstanceState);
                    devicePolicy.PutExtra(DevicePolicyManager.ExtraAddExplanation,
                        "Enable device administration for this app");
                    StartActivityForResult(devicePolicy,909);
                }
                catch (Exception p)
                {
                    Toast.MakeText(this,p.Message,ToastLength.Long).Show();
                }
            if (_devicePolicyManager.IsAdminActive(_deviceAdmin) == true)
            {
                try
                {
                    _devicePolicyManager.LockNow();
                }
                catch (Exception m)
                {
                    Toast.MakeText(this, m.Message,ToastLength.Long).Show();
                }
            }
            IntentFilter filter = new IntentFilter(Intent.ActionScreenOn);
            //register the receiver
            RegisterReceiver(_lockReceiver, filter);
           
        }

CodePudding user response:

I don't known whether I understand your request clearly. But you can try to check this link which is about native android.

Official document: https://developer.android.com/guide/topics/admin/device-admin#enabling

Similar case in native:Android Device Administrator permission activity not launching

With the code in xamarin.android:

 Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
 intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, _deviceAdmin);
 intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "some description");
 StartActivityForResult(intent, ACTIVATION_REQUEST);//ACTIVATION_REQUEST is a custom int value

Update:

The code about receiver:

 [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 AdminReceiver : DeviceAdminReceiver
{
    public override void OnEnabled(Context context, Intent intent)
    {
        base.OnEnabled(context, intent);
        Toast.MakeText(context, "Device Admin Enabled", ToastLength.Short).Show();

    }

    public override void OnLockTaskModeEntering(Context context, Intent intent, string pkg)
    {
        Toast.MakeText(context, "'Device entering locked mode", ToastLength.Long).Show();
    }

    public override void OnLockTaskModeExiting(Context context, Intent intent)
    {

    }

    public override void OnDisabled(Context context, Intent intent)
    {
        Toast.MakeText(context, "Admin mode disabled", ToastLength.Long).Show();
    }
}

The code about xml:

<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>

The code in my OnCreate method:

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        ComponentName _deviceAdmin = new ComponentName(this, Java.Lang.Class.FromType(typeof(AdminReceiver)));

        Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
        intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, _deviceAdmin);
        intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "some description");
        StartActivityForResult(intent, 47);//ACTIVATION_REQUEST is a custom int value
    }

The permission in the AndroidManifest.xml:

<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
  • Related