I need to warn the user about some events with:
Vibration must remain undefined until the user acknowledges the notification.
The problem is stopping the vibration when the device goes into sleep mode. I read the following questions:
Allow the phone to vibrate when the screen turns off
Continue to vibrate even after the screen goes to sleep in Android
There was an answer to one of the above questions, which said that vibration without patterns did the trick. So I tried calling the version of Vibrator.vibrate , which takes milliseconds instead of a large number pattern, but the vibration will stop anyway.
Other answers suggest registering the receiver in ACTION_SCREEN_OFF action. This would allow me to resume vibration if the device goes into sleep mode after starting the alarm, but it wonβt work if the device has already slept.
However, I could get this to work if I could turn on the screen first and then register the receiver to deal with any event that might happen from there. Therefore, I tried to get a full lock after a trigger event was received before triggering a sound or vibration, but it does not work even though I use the FULL_WAKE_LOCK and ACQUIRE_CAUSES_WAKEUP flags. Part of the awakening works, but soon after that the device will fall asleep again. I would like to think that the FULL_WAKE_LOCK flag does not work, since it is deprecated in API 17, but Samsung 4.1..2, which is API 16, works on my device!
The recommended approach now apparently uses WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON , but this should be called from the action, and I don't have a screen if the user does not click on the notification, and if this happens, the sound and vibration should already be stopped.
So this seems like a dead end.
What else could I try?
UPDATE:
I was not lucky that the screen was always turned on with wake locks, but on the other hand, they allow me to turn on the screen, if only for a few seconds. Actually, I donβt need to turn on the screen, so I register the receiver in the Intent.ACTION_SCREEN_OFF action, and when the screen turns off, the receiver resumes vibration again. This had a good effect on Samsung, but now I switched to Huawei to continue testing, and the receiver does not work.
UPDATE:
Here's the exception stack trace on a Huawei device:
java.util.NoSuchElementException: Death link does not exist at android.os.BinderProxy.unlinkToDeath(Native Method) at com.android.server.VibratorService.unlinkVibration(VibratorService.java:294) at com.android.server.VibratorService.removeVibrationLocked(VibratorService.java:284) at com.android.server.VibratorService.cancelVibrate(VibratorService.java:213) at android.os.IVibratorService$Stub.onTransact(IVibratorService.java:83) at android.os.Binder.execTransact(Binder.java:338) at dalvik.system.NativeStart.run(Native Method)