我有一个叫振动器的服务。按下电源按钮后振动器不振动?
[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和_wakeLock –
它被重现,但一旦我按下电源按钮,屏幕关闭 - 振动停止工作。 –