2

バイブレーションのパターンと繰り返しなしの -1 を渡しました。しかし、-1 の代わりに 10 を渡したいのですが (10 回繰り返す必要があります)、このパターンは 10 回繰り返されません。これを行う方法は?現在、私はこのコードを使用して

Vibrator mVibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long pattern[]={0,800,200,1200,300,2000,400,4000};
// 2nd argument is for repetition pass -1 if you do not want to repeat the Vibrate
mVibrate.vibrate(pattern,-1);

しかし、私はこれをやりたいのですが、うまくいき mVibrate.vibrate(pattern,10);ません。

4

3 に答える 3

3

これは期待どおりに機能します。2 番目のパラメーターのドキュメントを参照してください。

繰り返すパターンのインデックスを繰り返すか、繰り返したくない場合は -1 を指定します。

パターンにはインデックス10がないため、無視されます

于 2013-06-20T07:36:47.200 に答える
1

あなたの質問は、「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);

そして最後に、これを行うためのメソッドを 1 つだけ書きたい場合は (当然のことですが)、次のように記述できます。

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);
}
于 2015-10-06T18:04:19.693 に答える
1

Vibrator API でこのようなことを行う可能性はありません。考えられる解決策は、独自のリスナーを作成し、パターン[]が通過したなど、振動する頻度を数えることです。しかし、どうすればよいかわかりません....おそらく、あなたのパターン[]合計* 10とまったく同じ長さのタイマーで可能です

于 2013-06-20T07:43:05.083 に答える