gpt4 book ai didi

xamarin - 按下电源按钮后振动器不振动?

转载 作者:行者123 更新时间:2023-12-01 12:28:39 26 4
gpt4 key购买 nike

我有一个调用振动器的服务。

[Service]
public class MyService : Service
{
System.Threading.Timer timer;

public MyService()
{
}

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{

timer = new System.Threading.Timer( getCurrentPendingObjects, null, 100, Timeout.Infinite );

return StartCommandResult.Sticky;
}

public override bool OnUnbind( Intent intent )
{
return base.OnUnbind( intent );
}

public override IBinder OnBind(Intent intent)
{
binder = new MyServiceBinder( this );

return binder;
}

private void getCurrentPendingObjects( Object state )
{
Vibrator vibrator = (Vibrator)context.GetSystemService( Context.VibratorService );

long [] vibrationPatern = new long[ ] { 100, 170 };
vibrator.Vibrate( vibrationPatern, 1 );
}
}

一切正常,直到我按下电源按钮导致屏幕关闭,设备进入休眠模式。服务似乎继续工作,但振动器停止振动。

  • 如何在设备进入休眠模式的情况下保持振动器振动?

最佳答案

唤醒锁 将使系统保持事件状态,例如:

PowerManager powerManager = (PowerManager)Application.Context.GetSystemService(Application.PowerService);
Android.OS.PowerManager.WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial, "StackOverFlow");
wakeLock.Acquire();

注意:完成后,请释放锁定,因为这会降低电池生命周期:

wakeLock.Release();

注意: list 所需的权限

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

安卓文档:https://developer.android.com/reference/android/os/PowerManager.html

关于xamarin - 按下电源按钮后振动器不振动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36838524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com