Android Vibration while locked/sleep

2019-09-08 17:24发布

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条回答
三岁会撩人
2楼-- · 2019-09-08 18:13

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.

查看更多
登录 后发表回答