Ваш вопрос: «Как долго вибрировать Android-устройство (например, 2,5,10 минут)? Никто не ответил на ваш вопрос, поэтому я попробую. Я написал следующий код, чтобы вибрировать телефон на неопределенное время:
// get a handle on the device's vibrator
// should check if device has vibrator (omitted here)
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// create our pattern for vibrations
// note that the documentation states that the pattern is as follows:
// [time_to_wait_before_start_vibrate, time_to_vibrate, time_to_wait_before_start_vibrate, time_to_vibrate, ...]
// the following pattern waits 0 seconds to start vibrating and
// vibrates for one second
long vibratePattern[] = {0, 1000};
// the documentation states that the second parameter to the
// vibrate() method is the index of your pattern at which you
// would like to restart **AS IF IT WERE THE FIRST INDEX**
// note that these vibration patterns will index into my array,
// iterate through, and repeat until they are cancelled
// this will tell the vibrator to vibrate the device after
// waiting for vibratePattern[0] milliseconds and then
// vibrate the device for vibratePattern[1] milliseconds,
// then, since I have told the vibrate method to repeat starting
// at the 0th index, it will start over and wait vibratePattern[0] ms
// and then vibrate for vibratePattern[1] ms, and start over. It will
// continue doing this until Vibrate#cancel is called on your vibrator.
v.vibrate(pattern, 0);
Если вы хотите, чтобы вибрировать устройство для 2, 5 или 10 минут, вы можете использовать этот код:
// 2 minutes
v.vibrate(2 * 60 * 1000);
// 5 minutes
v.vibrate(5 * 60 * 1000);
// 10 minutes
v.vibrate(10 * 60 * 1000);
И, наконец, , если вы просто хотите написать один способ сделать это (как следует), вы можете написать следующее:
public void vibrateForNMinutes(Vibrator v, int numMinutesToVibrate) {
// variable constants explicitly set for clarity
// milliseconds per second
long millisPerSecond = 1000;
// seconds per minute
long secondsPerMinute = 60;
v.vibrate(numMinutesToVibrate * secondsPerMinute * millisPerSecond);
}
Теперь я прошел 300, который доступен в моем шаблоне, но его не вибрирующий ... –
мы говорим об индексе , поэтому int [4] arr имеет индекс: 0.1 2 3 – pskink
теперь я передал индекс как 7 и его вибрацию за один раз (от 20 до 25 секунд), пока образец не будет работать, но мой вопрос был ... как вибрировать его на 5 или 10 минут? –