2013-03-11 30 views
0

我有三个功能 - 动画,声音,振动。所有这些工作完全罚款个别。我需要一起开始所有这三项功能,然后继续播放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 
} 
+0

你可以使用countdownTimer。,对于每个tick)ie)你可以设置每个tick 15秒,并在onTick()method.right? – 2013-03-11 11:25:48

+0

@Tamilselvan你能提供一个关于如何实现这个的代码片段吗?我对使用线程n定时器知之甚少。谢谢。 – newbee 2013-03-11 11:31:40

+1

@newtoandroid你为什么重复? http://stackoverflow.com/questions/15335791/perform-functions-synchronously你已经有3个答案。 – 2013-03-11 11:57:11

回答

0

需要实现线程一起出马指定时间。

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