Reputation: 5434
I have a strange issue with the vibrator object.
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2 * DateUtils.SECOND_IN_MILLIS);
When the screen is on (even out of my app), it works just as expected. But if I turn the screen off by pressing once the power button, the vibrator does not stop vibrating after the 2 seconds. It continues to vibrate indefinitely.
Note that this behavior only happens on my Motorola Moto E3. Do you have any idea how I can circumvent this?
Upvotes: 0
Views: 2571
Reputation: 5434
I tried everything (postdelayed, services, new thread, ...), without any success. But I finally found a way to circumvent this bug on the Motorola Moto E3, using a pattern :
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(new long[]{0, 2 * DateUtils.SECOND_IN_MILLIS}, -1);
I hope this helps.
Upvotes: 0
Reputation: 5830
If what Roy proposed doesn't work. Try making a AlarmManager, and set it to go of in 2 seconds, and call vibrator.cancel() inside of it
Upvotes: 1
Reputation: 15615
As per my experience, the Vibrator
works differently on different devices. The best idea is to cancel()
the vibration in onStop()
of your Activity.
Upvotes: 1