Jump to content

Compte à rebours sur Android


raffi3438
 Share

Recommended Posts

Bonjour, je voudrais faire un compte à rebours sur Android

J'ai d'abord commencé par ce code (qui fonctionne parfaitement) qui affiche lors du lancement de l'application le temps restant jusqu'à la date fin

package com.example.muselaw;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.TextView;

import java.util.Calendar;

public class MuseLaw extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_muse_law);

TextView horloge = (TextView)findViewById(R.id.horloge);

long diff;

long milli;

long sec;

long min;

long heure;

long jour;

Calendar fin = Calendar.getInstance();

fin.set(2012, 9, 1, 0, 0, 0);

Calendar maintenant;

maintenant = Calendar.getInstance();

diff = fin.getTimeInMillis() - maintenant.getTimeInMillis();

milli = diff % 1000;

sec = ((diff - milli)/ 1000) % 60;

min = ((diff - milli - sec)/ (1000 * 60) % 60);

heure = ((diff - milli - sec - min)/ (1000 * 60 * 60) % 24);

jour = ((diff - milli - sec - min - heure)/ (1000 * 60 * 60 * 24));

horloge.setText(String.valueOf(jour) + " : " + String.valueOf(heure) + " : " + String.valueOf(min) + " : " + String.valueOf(sec) + " : " + String.valueOf(milli));

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_muse_law, menu);

return true;

}

}

Puis j'ai voulu améliorer mon code pour le compte à rebours continu à se décompter une fois l'appli lancé, j'ai donc mis une boucle infinie mais ça n'affiche rien, savez vous pourquoi (peut être qu'il ne faut pas faire de boucle infinie dans Android)?

Merci

package com.example.muselaw;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.TextView;

import java.util.Calendar;

public class MuseLaw extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_muse_law);

TextView horloge = (TextView)findViewById(R.id.horloge);

long diff;

long milli;

long sec;

long min;

long heure;

long jour;

Calendar fin = Calendar.getInstance();

fin.set(2012, 9, 1, 0, 0, 0);

Calendar maintenant;

while(true){

maintenant = Calendar.getInstance();

diff = fin.getTimeInMillis() - maintenant.getTimeInMillis();

milli = diff % 1000;

sec = ((diff - milli)/ 1000) % 60;

min = ((diff - milli - sec)/ (1000 * 60) % 60);

heure = ((diff - milli - sec - min)/ (1000 * 60 * 60) % 24);

jour = ((diff - milli - sec - min - heure)/ (1000 * 60 * 60 * 24));

horloge.setText(String.valueOf(jour) + " : " + String.valueOf(heure) + " : " + String.valueOf(min) + " : " + String.valueOf(sec) + " : " + String.valueOf(milli));

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_muse_law, menu);

return true;

}

}

Link to comment
Share on other sites

C'est bon j'ai réussi grâce à un objet tout prêt dans android MyCount

Voici mon code:

package com.example.muselaw;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.os.CountDownTimer;
import java.util.Calendar;

public class MuseLaw extends Activity {
TextView horloge = null;
long milli;
long sec;
long min;
long heure;
long jour;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_muse_law);
horloge = (TextView)findViewById(R.id.horloge);
Calendar fin = Calendar.getInstance();
fin.set(2012, 9, 1, 1, 0, 0);
Calendar maintenant;
maintenant = Calendar.getInstance();
long diff = fin.getTimeInMillis() - maintenant.getTimeInMillis();
MyCount counter = new MyCount(diff,1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onfinish() {
horloge.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
milli = millisUntilFinished % 1000;
sec = ((millisUntilFinished - milli)/ 1000) % 60;
min = ((millisUntilFinished - milli - sec)/ (1000 * 60) % 60);
heure = ((millisUntilFinished - milli - sec - min)/ (1000 * 60 * 60) % 24);
jour = ((millisUntilFinished - milli - sec - min - heure)/ (1000 * 60 * 60 * 24));
horloge.setText(String.valueOf(jour) + "J " + String.valueOf(heure) + "H " + String.valueOf(min) + "m " + String.valueOf(sec) + "s");
}
}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...