我使用BroadcastReceiver
To:
Activity
当Sleep mode
BUT
当Sleep mode
中的手机 时(手机已锁定且屏幕关闭),屏幕会亮起,Activity
显示但< / strong>振动服务在OnDestroy
之后立即转到onStartCommand
!
我的BroadcastReceiver:
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 中的Play()
,我的手机振动了10秒钟。
代码:
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秒)是:
- 失败:睡眠模式=&gt;倒计时
Activity
----开始----&gt;TimerTimeoutBroadcastReceiver
- 失败:睡眠模式=&gt; A
Service
----开始----&gt;TimerTimeoutBroadcastReceiver
- 失败:正常模式=&gt; A
Service
----开始----&gt;TimerTimeoutBroadcastReceiver
- 成功:正常模式=&gt;倒计时
Activity
----开始----&gt;TimerTimeoutBroadcastReceiver
最后,我想从我的APP中的TimerTimeoutBroadcastReceiver
开始Service
,但这个问题让我很困惑!
答案 0 :(得分:0)
问题解决了!我的服务(以及通知)确实有效,但我在onPause
的{{1}}中有这些代码:
Activity
我protected void onPause() {
Intent playAlarm = new Intent(this, AlarmVibrationService.class);
stopService(playAlarm);
super.onPause();
}
在睡眠模式下打开后,它(onPause
)就会发生!我现在不知道为什么会这样,但它会导致停止服务。现在我删除了上面的代码,问题解决了......