我有一個叫振動器的服務。按下電源按鈕後振動器不振動?
[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 –
它被重現,但一旦我按下電源按鈕,屏幕關閉 - 振動停止工作。 –