如何使用C#Xamarin模拟Android中的电源按钮?

时间:2019-07-03 12:15:32

标签: c# android .net xamarin

我想编写一个小应用程序,以便在启动时开始计数100秒,然后模拟单击电源按钮。使设备进入睡眠状态。如何使用C#Xamarin模拟Android中的电源按钮单击?感谢前进

1 个答案:

答案 0 :(得分:0)

根据您的描述,您想要锁定屏幕,使用设备管理器DevicePolicyManager锁定屏幕,然后使用PowerManager显示屏幕。

  1. 继承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和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. 检查屏幕状态

      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");
    
        }
    
    }
    

请在mainifest.xml中确认权限:

<uses-permission android:name="android.permission.DEVICE_POWER" />
<uses-permission android:name="android.permission.WAKE_LOCK" />