2013-03-11 60 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 
     } 
}