我有一个叫振动器的服务。
[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 );
}
}
一切正常,直到我按下电源按钮导致屏幕关闭,设备进入睡眠模式。 服务似乎继续工作,但振动器停止振动。
答案 0 :(得分:2)
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文档:https://developer.android.com/reference/android/os/PowerManager.html
答案 1 :(得分: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();
答案 2 :(得分:1)
如果您使用BroadcastReceiver,则可以连接到屏幕关闭操作。我可以在我的设备上启动服务,如下所示。
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);