0
我創建了一個CountDown計時器,我使用此代碼。 (先抱歉我的英文不好)。我使用SharedPreferences來保存我去另一個活動並返回的時間,但是當我返回到倒計時活動時,它重新啓動計時器並不保存任何內容。你能幫我嗎?使用的代碼就是這些!SharedPreferences和倒數計時器:我不能節省時間
public class Main8Activity extends Activity implements OnClickListener {
Myapp application ;
String eleg="";
CountDownTimer start1;
Button contador;
long cont=0,q=0;
private SimpleDateFormat timeFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.fragment8_main);
timeFormat = new SimpleDateFormat("HH:mm:ss");
Button seg= (Button) findViewById(R.id.button1);//seguir
seg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//el a
temporizador(q);
}
});
Button par= (Button) findViewById(R.id.button2);//seguir
par.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//el a
parar();
}
});
//te
//temporizador(final long secondadd);
temporizador(600000);
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
public void temporizador(final long secondadd){
start1 = new CountDownTimer(secondadd, 1000) {
//se ejecuta al terminar el tiempo,cuando llega a 0
@Override
public void onFinish() {
//contador.setText(String.valueOf(00+":"+00));
contador.setText("Time Elapsed: " + timeFormat.format(secondadd));
Toast.makeText(Main8Activity.this, "Se terminó el tiempo", Toast.LENGTH_SHORT).show();
cont=0;
}
//se ejecuta por cada segundo pasado
@Override
public void onTick(long millisUntilFinished) {
contador.setText("Time Elapsed: " + timeFormat.format(secondadd));
contador.setText(String.format(timeFormat.format(millisUntilFinished)));
// contador.setText( String.format("%02d:%02d:%02d", millisUntilFinished/3600,(millisUntilFinished % 3600)/60, (millisUntilFinished % 60)));
cont=millisUntilFinished;
q= millisUntilFinished;
}
}.start();
}
//metodo parar el cual detiene el tiempo del contador
public void parar(){
start1.cancel();
}
//metodo ondestroy
@Override
protected void onDestroy() {
super.onDestroy();
parar();
}
//metodo onstop
@Override
protected void onStop() {
super.onStop();
//se rescata el tiempo en el que se quedo
// int tiempokedo=Integer.parseInt((contador.getText()+"000").toString());
//el tiempo se guarda en un preferences
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor=preferences.edit();
editor.putLong("stiem",cont);
editor.commit();
//se detiene el temporizador(tiempo)
parar();
}
//metodo onrestart
protected void onRestart() {
super.onRestart();
//se rescata de preferencias el valor del tiempo el cual se quedo
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(this);
int tr=preferences.getInt("stiem",0);//re
//se envia al temporizador con el parametro rescatado para que siga el tiempo
temporizador(tr);
}
}