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

Posted mschen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于xamarin/xamarin.forms 在锁屏电源唤醒时保持后台运行的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.Forms踩坑集锦(持续更新)

一种在锁屏安卓中捕捉双击的方法?

如何在锁屏上显示通知内容 - Android

Android 11.0 修复在锁屏界面连续按锁屏键,需要按多次空格键才能解锁的Bug

Windows技巧|如何在Windows 10在锁屏状态下打开某种应用程序?

iOS Wikitude SDK (5.1.4) 在锁屏时崩溃