2011-12-01 3 views
2

j'ai besoin d'inclure le minuteur dans mon application.je viens de passer par l'exemple donné dans les sites.et essayé d'implémenter cela dans mon programme.mais la chose est que je ne sais pas comment travailler avec timer.in mon programme je veux démarrer la minuterie sur un bouton click.timer doit montrer dans l'écran. mais l'écran est une toile (lorsque nous cliquons sur le bouton, il charge une toile) avec des objets en mouvement.quand deux objets entrent en collision doit arrêter et doit montrer le valeur dans un message box.given ci-dessous est mon code d'activité principal.comment inclure la minuterie dans mon programme?

public class MyActivity extends Activity { 

private static final String TAG = MyActivity.class.getSimpleName(); 
public static Timer myTimer; 
private Button startbtn; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main); 
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    startbtn=(Button)findViewById(R.id.startbtn); 
    startbtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

       // making it full screen 
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
       // set our MainGamePanel as the View 
       setContentView(new MainGamePanel(getApplicationContext())); 
       Log.d(TAG, "View added"); 

     } 
    }); 

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

    }, 0, 1000); 

} 

@Override 
protected void onDestroy() { 
    Log.d(TAG, "Destroying..."); 
    super.onDestroy(); 
} 

@Override 
protected void onStop() { 
    Log.d(TAG, "Stopping..."); 
    super.onStop(); 
} 
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 

    } 
}; 

     } 

je sais comment vérifier collision.i besoin qu'une partie de la minuterie comme le démarrage d'horloge, montrant à l'écran et l'arrêt d'une autre personne class.can me aider ... plz ....

+0

votre code a déjà la tâche minuterie et minuterie implémentée. Donc quel est le problème? – blessenm

+0

ijust a copié ce code à partir d'un site. je dnt knw comment puis-je faire les choses restantes comme montrant la minuterie dans l'écran et l'arrêt lorsque les objets entrent en collision .. –

+0

personne n'a de réponse à ma question ?????? :( –

Répondre

1

Utilisez ceci le code sera répété après une seconde et la minuterie fonctionnera pendant 30sec

new CountdownTimer(30000, 1000) { 

public void onTick(long millisUntilFinished) { 
    mTextField.setText("seconds remaining: " + millisUntilFinished/1000); 
} 

public void onFinish() { 
    finish(); 
    } 
}.start(); 
Questions connexes