Forum Cross Platform with Xamarin

could someone help me with DevicePolicyManager.LockNow(); to work?

Hello, i´m new here. I need some help with an issue. I´m working with a button that when you touch it the screen goes into lock mode. I´m working with xamarin native (android platform, not forms). My issue is with policies i think, this is the exception message: "Java.Lang.SecurityException: 'No active admin owned by uid 10088 for policy #3'", and i share my code:
using System;
using Android.App;
using Android.App.Admin;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Widget;

namespace MyGuard.Droid.Views
{
[Activity(Label = "Guard", NoHistory = true, ScreenOrientation = ScreenOrientation.Portrait)]

public class Guard : Activity
{
    #region Private Global Variables

    Button btnStart;
    private DevicePolicyManager policyManager;
    private ComponentName adminReceiver;
    private PowerManager mPowerManager;

    #endregion
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Guard);
        btnStart = FindViewById<Button>(Resource.Id.btnStart);
        btnStart.Click += BtnStart_Click;
        adminReceiver = new ComponentName(this, Java.Lang.Class.FromType(typeof(Guard)).Name);
        policyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
        mPowerManager = (PowerManager)GetSystemService(Context.PowerService);
    }

    private void BtnStart_Click(object sender, EventArgs e)
    {
        policyManager.LockNow();

    }


}

}

thanks a lot!

Best Answer

Answers

  • matutematute Member ✭✭

    thank´s a lot LeonLu, i will try this and let you know the result!

  • matutematute Member ✭✭

    You Rock! LeonLu, perfect solution, thank you!!

  • Hi LeonLu and experts!
    Thanks for this solution, it works well except Android 10 and Android 11
    In Android 9-, I call lockNow() in a timer then if the app is inactive on screen, it CAN lock the device perfectly.
    In Android 10 and Android 11, I call lockNow() in a timer then if the app is INactive on screen, it will NOT able to lock the device.
    How can I make the lockNow() in timer works in Android 10+11 while the app screen is inactive?

Sign In or Register to comment.