2016-04-25 81 views
2

我有一個叫振動器的服務。按下電源按鈕後振動器不振動?

[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); 
    } 
} 

一切正常,直到我按下電源按鈕導致屏幕關閉,設備進入睡眠模式。 該服務似乎繼續工作,但振動器停止振動。

  • 如何保持振動器振動,即使設備進入睡眠模式?

回答

1

嗯好像你正在...一個有趣的應用程序:P

我認爲,保持服務運行,你需要一個WakeLockhttps://developer.xamarin.com/api/type/Android.OS.PowerManager+WakeLock/

一個喚醒鎖是一種機制,表明您的應用程序需要讓設備保持開啓狀態。

任何使用WakeLock的應用程序都必須在應用程序清單的元素中請求android.permission.WAKE_LOCK權限。通過調用PowerManager.NewWakeLock(WakeLockFlags,String)來獲得喚醒鎖。

調用PowerManager + WakeLock.Acquire獲取喚醒鎖並強制設備保持在創建喚醒鎖時請求的級別。

呼叫PowerManager + WakeLock.Release完成後不再需要鎖。儘快做到這一點非常重要,以避免過度使用設備的電池。

您可能能夠做到這一點:當你完成像這樣

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

    private PowerManager _powerManager; 
    private PowerManager.WakeLock _wakeLock; 

    public MyService() 
    { 
    } 

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
    { 
     _powerManager = (PowerManager) GetSystemService(PowerService); 
     _wakeLock = _powerManager.NewWakeLock(WakeLockFlags. Partial, "Vibrator"); 
     _wakeLock.Acquire(); 

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

     return StartCommandResult.Sticky; 
    } 

    public override bool OnUnbind(Intent intent) 
    { 
     if (_wakeLock.IsHeld) 
      _wakeLock.Release(); 
     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); 
    } 
} 

你也需要在清單

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

此權限和釋放:

if (_wakeLock.IsHeld) 
     _wakeLock.Release(); 
+0

我只是做了你的建議伊恩·史密斯,但一旦我按下電源按鈕振動器不會再打電話時,屏幕被喚醒後,即使服務。 –

+0

只是更新我的代碼忘了創建_powerManager和_wakeLock –

+0

它被重現,但一旦我按下電源按鈕,屏幕關閉 - 振動停止工作。 –

1

Wake locks將保持系統活着,是這樣的:

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

注意:您完成後,解除鎖定,因爲這會降低電池的使用壽命:

wakeLock.Release(); 

注:清單所需的權限

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

的Android Docs:https://developer.android.com/reference/android/os/PowerManager.html

1

如果您使用BroadcastReceiver,您可以連接到屏幕O ff行動。在那裏我可以啓動服務,如下所示,在我的設備上運行。

public class MyReceiver : BroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     if (intent.Action.Equals(Intent.ActionScreenOff)) 
     { 
      Application.Context.StartService(new Intent(Application.Context, typeof(MyService))); 
     } 
    } 
} 

只需註冊接收器:

IntentFilter filter = new IntentFilter(Intent.ActionScreenOff); 
RegisterReceiver(new MyReceiver(), filter); 
+0

該服務在設備啓動時啓動,並且與其關愛應用程序一起啓動。 服務工作正常,我相信。 –

+0

也許你有更多的代碼。在我的設備上,它處於開啓和關閉模式下振動。 – Matt