2012-02-10 19 views
0

我用振动服务在Android和设置时间= 500毫秒,如下为什么我的应用程序不断震动?

Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);   

long milliseconds = 500; 

v.vibrate(milliseconds); 

有时它的工作确定,但有时我不知道为什么它不断地振动,只有当我打开屏幕停止。 我的应用程序适用于Android 2.2。

请帮帮我。

非常感谢。

/**编辑*/

感谢您的答复,

我开始报警服务内部振动。每当警报响起时,我会播放声音,启动这个服务中的振动。这里是我的代码

Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); 
long milliseconds = 500; 
v.vibrate(milliseconds); 

MediaPlayer mp = MediaPlayer.create(this, R.raw.normal); 
mp.start(); 
mp.setOnCompletionListener(new OnCompletionListener() { 
    public void onCompletion(MediaPlayer mp) { 
     // TODO Auto-generated method stub 
     mp.release(); 
    } 
}); 
PowerManager pm = (PowerManager) this 
      .getSystemService(Context.POWER_SERVICE); 
mWakelock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK 
      | PowerManager.ACQUIRE_CAUSES_WAKEUP, "AlarmScreen"); 
mWakelock.acquire(120000); 

的问题是,有时它工作正常,但有时它不断地振动,只有当我打开屏幕停止。

回答

1

尝试在这样的方式使用...

Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 
    v.vibrate(2000); 

删除:

this. 

,一切都会好起来的

+0

“这个”这里是我们的服务对象本身停止振动。我不认为这是我的问题。 – MichaelP 2012-02-10 09:53:22

+0

尝试mp.start前mp.release()()每次调用媒体播放器... – timonvlad 2012-02-10 13:22:52

+0

感谢您的时间,但我得到异常IllegalStateException异常时,我打电话mp.start(前mp.release()) – MichaelP 2012-02-13 02:20:33

0

的问题是不是你正在使用的代码振动设备,这可能是调用振动的代码。我猜你在某种循环,引发真实的振动是。你能显示用于开始振动的代码吗?

+0

我刚编辑我的问题,你可以看到它上面。谢谢 – MichaelP 2012-02-10 09:54:05

0

您可以强制使用延迟

final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
v.vibrate(2000); 

final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
@Override 
public void run() { 
    //Do something after 2000ms (avoid random vibrations) 
    v.cancel(); 
    } 
}, 2000); 
相关问题