我有几个功能必须同步实施3秒。我使用soundpool,振动模式和背景动画播放声音。他们三人都分开工作。但我无法正确计时,以便一起开始。同步执行功能
这3个函数需要每15秒重复调用一次。并会在点击按钮时停止。我试着用这样的东西,但它不能正常工作。
myThread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
while (shouldRun==true) {
try {
mHandler.post(new Runnable() {
@Override
public void run() {
anim();
vibration();
sound();
}
});
mHandler.postDelayed(new Runnable() {
public void run(){
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 3000);
} catch (Exception e) {
// TODO: handle exception
}
}
}// end of outer run()
});
myThread.start();
这是正确的方法吗?或者有其他方法吗?
public void vibration(){
int dash = 1000;
int medium_gap = 500;
long[] pattern = { 0, // Start immediately
dash, medium_gap,dash , medium_gap
};
// Only perform this pattern one time (-1 means "do not repeat")
v.vibrate(pattern, -1);
}
linear = (LinearLayout) findViewById(R.id.lay);// this line is after setContentView in onCreate
public void anim(){
drawable.addFrame(new ColorDrawable(Color.RED), 1000);
drawable.addFrame(new ColorDrawable(Color.WHITE), 500);
drawable.setOneShot(false);
linear.setBackgroundDrawable(drawable);
drawable.start();
}
sp = new SoundPool(5, AudioManager.STREAM_NOTIFICATION, 0);
buzzer = sp.load(this, R.raw.buzzer, 0);
public void sound(){
sp.play(buzzer, 1,1, 0, 0, 1); //start alert tone
}
@拉玛昌德拉·雷迪阿姆阿姆()是不帮助我。即使现在,我的动画卡住了,振动发生在一些奇怪的方式。 – newbee 2013-03-11 10:38:25