0

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

ignn
  • 29
  • 4
  • you might wanna take look at this: https://stackoverflow.com/questions/4545079/lock-the-android-device-programmatically – Alex Jul 03 '19 at 12:38
  • Possible duplicate of [Lock the Android device programmatically](https://stackoverflow.com/questions/4545079/lock-the-android-device-programmatically) – Cheesebaron Jul 03 '19 at 14:08

1 Answers1

0

According to your description, you want to lock screen, use the device manager DevicePolicyManager to lock screen and use PowerManager to display the screen.

  1. inherit DeviceAdminReceiver

    public class ScreenOffAdminReceiver: DeviceAdminReceiver
    {
    private void showToast(Context context,string msg)
    {
        Toast.MakeText(context, msg, ToastLength.Short).Show();
    
    }
    public override void OnEnabled(Context context, Intent intent)
    {
        base.OnEnabled(context, intent);
        showToast(context, "Device Manager enable");
    
    }
    
    public override void OnDisabled(Context context, Intent intent)
    {
        base.OnDisabled(context, intent);
        showToast(context, "Device Manager is not enabled");
    
    }
    

    }

  2. PowerManager and DevicePolicyManager

    private DevicePolicyManager policyManager;
    private ComponentName adminReceiver;
    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;
    
    
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
    
    
    
        adminReceiver= new ComponentName(this, Java.Lang.Class.FromType(typeof(ScreenOffAdminReceiver)).Name);
        policyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
    
        mPowerManager = (PowerManager)GetSystemService(Context.PowerService);
    }
    
  3. Check screen state

      private void BtncheckAndTurnOnDeviceManager_Click(object sender, System.EventArgs e)
         {
        Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
        intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, adminReceiver);
        intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "After you turn it on, you can use the lock screen function....");
        StartActivityForResult(intent, 0);
    
    }
    
    private void Btncheckscreenoff_Click(object sender, System.EventArgs e)
    {
        bool admin = policyManager.IsAdminActive(adminReceiver);
        if (admin)
        {
            policyManager.LockNow();
        }
        else
        {
            showToast("No device management permissions");
        }
    
    }
    
    private void Btncheckscreenon_Click(object sender, System.EventArgs e)
    {
        mWakeLock = mPowerManager.NewWakeLock(WakeLockFlags.Partial, "tag");
        mWakeLock.Acquire();
        mWakeLock.Release();
    
    }
    
    private void Btncheckscreen_Click(object sender, System.EventArgs e)
    {
        PowerManager pm = (PowerManager)GetSystemService(Context.PowerService);
        bool screenOn = pm.IsScreenOn;
        if (!screenOn)
        {
            showToast("The screen is a black screen");
        }
        else
        {
            showToast("The screen is bright");
    
        }
    
    }
    

Please confirm permission in mainifest.xml:

<uses-permission android:name="android.permission.DEVICE_POWER" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16