2012-04-23 72 views
3

我想實現一個功能到應用中它可能是脈衝振動。 用戶可以使用滑塊更改3種東西,振動強度,脈衝長度和脈衝之間的時間。遇到問題脈衝振動

我喜歡思考的一些代碼:

for(i=0; i<(pulse length * whatever)+(pulse gap * whatever); i+=1){ 
pattern[i]=pulse length*i; 
patern[i+1]=pulse gap; 

然而,當我使用此代碼(當它做得好,那只是一個簡單的例子),它崩潰的應用程序。此外,當我改變振動強度(這是行不通的),我不得不重新啓動服務,以改變力量。我改變力量的方法是改變振動器打開的時間,並以一種模式關閉。

這是我使用用於檢測手機是否振動代碼(在這裏的代碼是什麼,我寧願有一點不同):

if (rb == 3){ 
    z.vibrate(constant, 0); 
} else if (rb == 2){ 
    smooth[0]=0; 
    for (int i=1; i<100; i+=2){ 
      double angle = (2.0 * Math.PI * i)/100; 
      smooth[i] = (long) (Math.sin(angle)*127); 
      smooth[i+1]=10; 
    } 
    z.vibrate(smooth, 0); 
} else if (rb == 1){ 
    sharp[0]=0; 
    for(int i=0; i<10; i+=2){ 
      sharp[i] = s*pl; 
      sharp[i+1] = s+pg; 
    } 
    z.vibrate(sharp, 0); 
} 
} else { 
     z.cancel(); 
} 

如果任何人能指出我的方向一些代碼可以做到這一點,或者我可以做到這一點,我非常感謝。

+0

請張貼您的錯誤跟蹤。 – Sam 2012-04-23 15:03:52

回答

0

我唯一的猜測是,您收到一個ArrayIndexOutOfBounds錯誤。

如果是這樣,你需要在試圖填充它們之前定義你的long數組的長度。

long[] OutOfBounds = new long[]; 
OutOfBounds[0] = 100; 
// this is an error, it's trying to access something that does not exist. 

long[] legit = new long[3]; 
legit[0] = 0; 
legit[1] = 500; 
legit[2] = 1000; 
// legit[3] = 0; Again, this will give you an error. 

vibrate()是一個聰明的功能雖然。這兩個示例都不會引發錯誤:

v.vibrate(legit, 0); 
// vibrate() combines both legit[0] + legit[2] for the 'off' time 

long tooLegit = new long[100]; 
tooLegit[0] = 1000; 
tooLegit[1] = 500; 
tooLegit[10] = 100; 
tooLegit[11] = 2000; 
v.vibrate(tooLegit, 0); 
// vibrate() skips over the values you didn't define, ie long[2] = 0, long[3] = 0, etc 

希望有所幫助。