2012-04-23 76 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 

希望有所帮助。