2013-06-20 38 views
2

我已經通過振動模式和-1沒有重複。但我想通過10代替-1(現在它應該重複10倍),那麼這種模式是不是重複10 times.how做到這一點?我目前使用此代碼如何振動Android設備很長時間(例如2,5,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);這是行不通的。

回答

3

如預期那樣工作,看到第二個參數文檔:

重複 的索引處重複圖案,或者-1,如果你不想重複。

而且由於你的模式沒有指數10,它只是忽略

+0

現在我通過300可在我的模式,但它不是振動... –

+0

我們說的關於索引,所以int [4] arr的索引是:0.1 2 3 – pskink

+0

現在我通過索引7並且振動一次(20到25秒)直到模式運行,但是我的問題是..如何振動它5或10分鐘 ? –

1

有振動API中沒有可能做這樣的事情。一個可能的解決方案可能是做你自己的聽衆,並計算它振動的頻率,例如模式[]已經過去了。但我不知道該怎麼做....也許這是可能的與一個計時器是完全一樣長的模式[]總和* 10

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); 

最後如果你想只寫一個方法來做到這一點(你應該),你可以寫:

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); 
} 
相關問題