2011-01-06 23 views
6

मेरे एंड्रॉइड एप्लिकेशन में मैं समय के नियमित अंतराल पर विशेष विधि को कॉल करना चाहता हूं। "हर 5 सेकंड के बाद" ... मैं यह कैसे कर सकता हूं ....?समय के नियमित अंतराल के बाद विशेष विधि कॉल करें

उत्तर

13

आप किसी विधि के निश्चित अवधि निष्पादन के लिए Timer का उपयोग कर सकते हैं।

यहाँ कोड का एक नमूना है:

final long period = 0; 
new Timer().schedule(new TimerTask() { 
    @Override 
    public void run() { 
     // do your task here 
    } 
}, 0, period); 
+1

अरे भाई ... धन्यवाद .... – Nirav

10

ऊपर इस लिंक की जांच कर रहा है और ठीक काम करता है। यह हर कोड कुछ विधि कॉल करने के लिए कोड है। आप बदल सकते हैं 1000 (= 1 सेकंड) किसी भी समय आप चाहते हैं (उदाहरण के लिए 3 सेकंड = 3000)

public class myActivity extends Activity { 

private Timer myTimer; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    myTimer = new Timer(); 
    myTimer.schedule(new TimerTask() {   
     @Override 
     public void run() { 
      TimerMethod(); 
     } 

    }, 0, 1000); 
} 

private void TimerMethod() 
{ 
    //This method is called directly by the timer 
    //and runs in the same thread as the timer. 

    //We call the method that will work with the UI 
    //through the runOnUiThread method. 
    this.runOnUiThread(Timer_Tick); 
} 


private Runnable Timer_Tick = new Runnable() { 
    public void run() { 

    //This method runs in the same thread as the UI.    

    //Do something to the UI thread here 

    } 
}; 
} 
+0

धन्यवाद। मेरे लिये कार्य करता है। – zwarrior

संबंधित मुद्दे