어떻게 사용자가 시간을 시작으로 카운트 다운 타이머를 구현하는 방법?

Nohnkhumr :

나는 간단한 텍스트보기 핸들러를 사용하여 타이머 다운 카운트를 구현하기 위해 노력 해왔다. 나는 사용자 정의 시간을 설정해야합니다. 전을 위해 . 타이머가에서 시작해야 6시 12분 0초 .

- 이것은이다 MainActivity의enter code here 코드 -.

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 -

    startButton.setOnClickListener(new View.OnClickListener() {

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

        }
    });

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

--listener 일시 정지에 대한 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);
    }

};}

내가 무엇입니까 출력은 여기 https://ibb.co/pd95Yww

VinothKumar Subramani :

공용 클래스 MainActivity는 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);

        }
    });

}

}

추천

출처http://43.154.161.224:23101/article/api/json?id=235764&siteId=1