按下电源按钮后振动器不振动?
我有一个叫振动器的服务。按下电源按钮后振动器不振动?
[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);
}
}
一切正常,直到我按下电源按钮导致屏幕关闭,设备进入睡眠模式。 该服务似乎继续工作,但振动器停止振动。
- 如何保持振动器振动,即使设备进入睡眠模式?
嗯好像你正在...一个有趣的应用程序:P
我认为,保持服务运行,你需要一个WakeLock
https://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();
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
如果您使用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);
该服务在设备启动时启动,并且与其关爱应用程序一起启动。 服务工作正常,我相信。 –
也许你有更多的代码。在我的设备上,它处于开启和关闭模式下振动。 – Matt
我只是做了你的建议伊恩·史密斯,但一旦我按下电源按钮振动器不会再打电话时,屏幕被唤醒后,即使服务。 –
只是更新我的代码忘了创建_powerManager和_wakeLock –
它被重现,但一旦我按下电源按钮,屏幕关闭 - 振动停止工作。 –