【问题标题】:Having trouble pulsing vibrator脉动振动器有问题
【发布时间】:2012-04-24 00:16:07
【问题描述】:

我正在尝试在我的应用中实现一项功能,它可以使振动器产生脉冲。 用户可以使用滑块更改 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();
}

如果有人能指出一些可以做到这一点的代码的方向,或者我如何使它工作,我将不胜感激。

【问题讨论】:

  • 请发布您的错误跟踪。

标签: java android vibration pulse


【解决方案1】:

我唯一的猜测是您收到 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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2021-09-26
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多