• xamarin/xamarin.forms 在锁屏电源唤醒时保持后台运行


    PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
    SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
    SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
    FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
    ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.
    ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间**

    方法(一)

     1 [BroadcastReceiver(Enabled = true)]
     2     public class AlarmService : BroadcastReceiver
     3     {
     4         //public event EventHandler WakeLockStatusChanged;
     5         public override void OnReceive(Context context, Intent intent)
     6         {
     7             PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
     8             PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "MyWakelockTag");
     9             wl.Acquire();
    10             //if (wl.IsHeld)
    11             //{
    12             //    wl.Release();
    13             //}
    14         }
    15     }
     1  private void InitBroadcastReceiver()
     2         {
     3             _alarmService = new AlarmService();
     4             Application.Context.RegisterReceiver(_alarmService, new IntentFilter(ConnectivityManager.ConnectivityAction));
     5         }
     6 
     7  protected override void OnStart()
     8         {
     9             base.OnStart();
    10             InitBroadcastReceiver();
    11         }
    12 
    13         protected override void OnStop()
    14         {
    15             base.OnStop();
    16 
    17             if (_alarmService != null)
    18             {
    19                 Application.Context.UnregisterReceiver(_alarmService);
    20                 _alarmService = null;
    21             }
    22 
    23         }

    方法(二)

     1    //获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行  
     2         private void AcquireWakeLock()
     3         {
     4             if (null == _wakeLock)
     5             {
     6                 PowerManager pm = (PowerManager)GetSystemService(Context.PowerService);
     7                 _wakeLock = pm.NewWakeLock(WakeLockFlags.Partial | WakeLockFlags.OnAfterRelease, "MyWakelockTag");
     8                 if (_wakeLock != null)
     9                 {
    10                     _wakeLock.Acquire();
    11                 }
    12             }
    13         }
    14 
    15         //释放设备电源锁  
    16         private void ReleaseWakeLock()
    17         {
    18             if (null != _wakeLock || _wakeLock.IsHeld)
    19             {
    20                 _wakeLock.Release();
    21                 _wakeLock = null;
    22             }
    23         }  

    参考资料:
    https://docs.microsoft.com/zh-cn/dotnet/api/Android.OS.PowerManager.NewWakeLock?view=xamarin-android-sdk-9#Android_OS_PowerManager_NewWakeLock_Android_OS_WakeLockFlags_System_String_

    https://stackoverflow.com/questions/54532221/prevent-app-from-going-on-onpause-when-turning-screen-off-using-partial-wakelock/54548392?r=SearchResults#54548392

    http://www.itgo.me/a/x9010099145016859770/xamarin-forms-broadcast-receiver-not-working-when-app-is-closed

  • 相关阅读:
    亚洲区哈尔滨赛区网络预选赛over
    背包问题
    Memcache基础教程
    Telnet的命令
    Telnet技术白皮书
    workthread模式
    Telnet的命令
    telnet 测试memcached
    telnet 测试memcached
    Memcache基础教程
  • 原文地址:https://www.cnblogs.com/mschen/p/11294665.html
Copyright © 2020-2023  润新知