我有一些功能必须同步实现 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
}