How to simulate a click on the power button in Android with C# Xamarin?

I want to write a small application so that when it starts, it starts counting for 100 seconds and then simulates a click on the power button. To send the device to sleep. How to simulate a click on the power button in Android with C# Xamarin? Thanks for advance

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can check this thread: https://stackoverflow.com/questions/8987847/devicepolicymanager-locknow-is-not-working-for-motorola-tablets

    The key code in c# is as follows:

       Button pressBtn;
    
        protected static int REQUEST_ENABLE = 0;
       public  DevicePolicyManager devicePolicyManager;
       public ComponentName adminComponent;
    
             private void PressBtn_Click(object sender, EventArgs e)
        {
    
            adminComponent = new ComponentName(this, Java.Lang.Class.FromType(typeof(Darclass)).Name);
            devicePolicyManager = (DevicePolicyManager) GetSystemService(Context.DevicePolicyService);
    
            if (!devicePolicyManager.IsAdminActive(adminComponent))
            {
                Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
                intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, adminComponent);
                StartActivityForResult(intent, REQUEST_ENABLE);
            }
            else
            {
                devicePolicyManager.LockNow();
            }
        }
    
Sign In or Register to comment.