我使用BroadcastReceiver
到:
- 叫醒电话
- 在服务中播放声音和振动
- 显示一个
Activity
当我在手机不在的情况下进行测试时,一切都运行良好Sleep mode
但
当手机在(Sleep mode
手机被锁定并且屏幕关闭)时,屏幕会打开并且Activity
显示良好但振动服务会OnDestroy
立即进入onStartCommand
!
我的广播接收器:
public class TimerTimeoutBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
WakeLockManager.acquireWakeLock(context);
Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeDialogs);
// Open OffAlarm Activity
StartAlarmOffActivity(context);
//StartVibrationService
StartVibrationService(context);
}
private void StartAlarmOffActivity(Context context) {
Intent alarmeActivity = new Intent(context, AlarmScreenOffActivity.class);
alarmeActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmeActivity);
}
private void StartVibrationService(Context context) {
Intent playAlarm = new Intent(context, AlarmSoundVibrationService.class);
context.startService(playAlarm);
}
}
在振动服务中:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
stopSelf();
return START_NOT_STICKY;
}
play();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
stop();
WakeLockManager.releaseWakeLock();
}
在 WakeLockManager 中:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE , "My Tag");
我的问题:
OnDestroy
当我在睡眠模式下启动服务时,如何防止立即呼叫?- 我应该使用通知来保持这项服务的活力吗?
编辑 1
我试图通过通知将该服务作为前台服务:
private void showNotification() {
CharSequence text = getText(R.string.alarm_vibration_service_started);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AlarmScreenOffActivity.class), 0);
// Set the info for the views that show in the notification panel.
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // the status icon
.setTicker(text) // the status text
.setWhen(System.currentTimeMillis()) // the time stamp
.setContentTitle(getText(R.string.alarm_vibration_service_label)+" "+mTimerTitle) // the label of the entry
.setContentText(text) // the contents of the entry
.setContentIntent(contentIntent) // The intent to send when the entry is clicked
.build();
}
// Send the notification.
startForeground(Alarm_Vibration_SERVICE_Id, notification);
}
现在我打电话给我showNotification
,onStartCommand
但它对我没有帮助!
使用后我成功Thread.Sleep(10000)
了,我在 onStartCommand 中使用了一个,我的手机振动了 10 秒。Play()
代码:
private void play() {
stop();
mVibrator.vibrate(sVibratePattern, 0);
enableTimeOut();
mPlaying = true;
}
public void stop() {
if (mPlaying) {
mPlaying = false;
mVibrator.cancel();
}
disableTimeOut();
}
private void enableTimeOut() {
mHandler.sendMessageDelayed(
mHandler.obtainMessage(TIMEOUT, true),
1000 * ALARM_TIMEOUT_SECONDS);
}
在某些情况下,我还测试了上述代码。结果(振动 10 秒)是:
- 失败: 睡眠模式=>
Activity
倒计时----开始---->TimerTimeoutBroadcastReceiver
- 失败: 睡眠模式 => A
Service
----开始---->TimerTimeoutBroadcastReceiver
- 失败: 正常模式 => A
Service
----开始---->TimerTimeoutBroadcastReceiver
- 成功:正常模式 =>
Activity
倒计时----开始---->TimerTimeoutBroadcastReceiver
最后我想TimerTimeoutBroadcastReceiver
从Service
我的APP中开始,但这个问题让我很困惑!