Android Vibration while locked/sleep

2019-09-08 17:33发布

问题:

Whether the user has the screen on or off I want to notify the user by a vibration. If the screen is on, this works well:

Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                            long sleep = 100;
                            long vibrate = 500;
                            long[] vibratePattern = {0, vibrate, sleep, vibrate, sleep};
                            vibrator.vibrate(vibratePattern, -1); 

Its called by a Thread that is implemented in a Service.

The Thread runs always even if the screen is off. I checked that because im implementing a timer. So that cant be the problem!

回答1:

Not sure if you have looked into Android's Notifications, but sounds like this would be perfect for what you are trying to achieve.

http://developer.android.com/intl/es/reference/android/app/Notification.Builder.html

when building a notification, you can cause your phone to vibrate with the setVibrate() method.