我正在嘗試在我的應用程式中實作一個可以使振動器產生脈沖的功能。用戶可以使用滑塊更改 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();
}
如果有人能夠指出一些可以做到這一點的代碼的方向,或者我如何使它作業,我將非常感激。
uj5u.com熱心網友回復:
我唯一的猜測是您收到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
希望有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/531320.html
標籤:爪哇安卓振动脉冲
上一篇:有沒有辦法使這個脈沖數無限?
