cómo implementar Temporizador de cuenta atrás con la costumbre hora de inicio?

Nohnkhumr:

He estado tratando de implementar un temporizador de cuenta atrás usando gestor en una vista de texto simple. Necesito programar otro tiempo. por ej ., el temporizador debe partir de 6:12:00 .

- Esta es la MainActivityenter code here código -.

public class MainActivity extends AppCompatActivity {
private Button startButton;
private Button pauseButton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     timerValue = (TextView) findViewById(R.id.timerValue);
     startButton = (Button) findViewById(R.id.startButton);

--listener para el botón de inicio -

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

--listener para una pausa button--

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

        }
    });
}

private Runnable updateTimerThread = new Runnable() {

    public void run()

{

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        int hours = mins/60;
        secs = secs % 60;

        timerValue.setText(String.format("%02d", hours)+":" + 
            (String.format("%02d", mins)) + ":"
                + String.format("%02d", secs) );
        customHandler.postDelayed(this, 0);
    }

};}

La salida que estoy recibiendo es aquí https://ibb.co/pd95Yww

VinothKumar Subramani:

MainActivity clase pública se extiende AppCompatActivity {

long timeInMilliseconds = 0L;
long timeSwapBuff = 22320000; // 6Hours:12Minutes:00Seconds
long updatedTime = 0L;
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
private Runnable updateTimerThread = new Runnable() {

    public void run() {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;
        String hms = String.format("%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(updatedTime),
                TimeUnit.MILLISECONDS.toMinutes(updatedTime) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(updatedTime)),
                TimeUnit.MILLISECONDS.toSeconds(updatedTime) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(updatedTime)));
        timerValue.setText(hms);
        customHandler.postDelayed(this, 0);
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timerValue = findViewById(R.id.timer);
    startButton = findViewById(R.id.start);
    pauseButton = findViewById(R.id.pause);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

        }
    });

}

}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=235770&siteId=1
Recomendado
Clasificación