I use a BroadcastReceiver
To:
- Wake up the phone
- Playing sound and vibration in a service
- Show an
Activity
Every things work well when i test it while the phone is not in Sleep mode
BUT
When the phone is in Sleep mode
(phone is locked and the screen is off), the screen goes ON and the Activity
is displayed well but Vibration Service goes to OnDestroy
immediately after onStartCommand
!
My 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);
}
}
In Vibration Service:
@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();
}
In WakeLockManager:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE , "My Tag");
My questions:
- How can i prevent call
OnDestroy
Immediately when i start my service in sleep mode? - Should i use a notification to keep this service alive?
Edit 1
I tried to take the service as a foreground service by a notification:
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);
}
Now i call showNotification
in onStartCommand
but it could not helped me!
I'm success when used i use a Thread.Sleep(10000)
in onStartCommand after Play()
and i have vibration on my phone for 10 second.
Codes:
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);
}
Also i tested above codes in some cases. The result (Vibration for 10 seconds) was:
- Failed: Sleep-Mode => An
Activity
with a countdown ----start---->TimerTimeoutBroadcastReceiver
- Failed: Sleep-Mode => A
Service
----start---->TimerTimeoutBroadcastReceiver
- Failed: Normal-Mode => A
Service
----start---->TimerTimeoutBroadcastReceiver
- Success: Normal-Mode => An
Activity
with a countdown ----start---->TimerTimeoutBroadcastReceiver
Finally I want start TimerTimeoutBroadcastReceiver
from a Service
in my APP but this problem confused me!