正确使用线程和计时器

时间:2013-03-11 11:19:14

标签: android

我有三个功能 - 动画,声音,振动。所有这些都完美无缺。我需要一起启动所有这三个功能并继续播放3秒然后停止。我希望每15秒后重复一次。实现这个的正确方法是什么? 我的振动代码如下

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();     
 }

声音使用soundpool:

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
}

1 个答案:

答案 0 :(得分:0)

你需要实现线程在指定时间内一起运行。

Thread myTime = new Thread()
{
      void run(){
           sleep(3000) //sleep untill 3 sec than stop 
           //your code
      }
}