Как использовать время TimepickerDialog

Мне нужно сделать обратный отсчет для моего мобильного приложения. Я использовал timePickerDialog кнопку и текстовое поле. Я использовал таймер обратного отсчета для обратного отсчета. Я взял системный час, но в то же время мне нужно взять выбранное время из TimePicker, но я не смог. Как можно получить время от timePickerDialog. Я вижу другое текстовое поле, и я могу использовать метод split, чтобы занять время, но я хочу сделать эту работу в фоновом режиме. Мой код ниже;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;


import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class Converter extends AppCompatActivity {

    // VARIABLES
    ImageButton startConvert;
    ImageButton stopConvert;
    TextView Downtxt;
    TextView topTxt;
    EditText Areatxt;
    Integer counter = 0;
    Integer timeHour = 5;
    Integer timeMinute = 50;
    Integer timeSecond = 0;
    Integer second = 0;
    Integer sysHour;
    Integer sysMinute;
    Integer total = 0;
    Integer countHour = 0;
    Boolean stop = false;
    Context context = this;
    static final int DIALOG_ID=0;


    private static final String FORMAT = "%02d:%02d:%02d";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_converter);
// Call
        startConvert = this.findViewById(R.id.imageButton);
        stopConvert = this.findViewById(R.id.imageButton2);
        Downtxt = this.findViewById(R.id.textView2);
        topTxt = this.findViewById(R.id.textView);
        Areatxt = this.findViewById(R.id.editText);

        //counter reset
        Downtxt.setText("00:00:00");

        //Voice to world




        //Start Button OnClick Methods

        startConvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                topTxt.setText("Listening..");

                //take system time
                Calendar now = Calendar.getInstance();
                sysHour = now.get(Calendar.HOUR);
                sysMinute = now.get(Calendar.MINUTE);
                second = now.get(Calendar.SECOND);


                //Record

             //Time Dialog
                if(counter==0) {




                    final TimePickerDialog myPickerDialog = new TimePickerDialog(context,
                            new TimePickerDialog.OnTimeSetListener() {

                                @Override
                                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                                   timeHour=hourOfDay;
                                   timeMinute = minute;
                                   //?????????????????????????????????????????????????????????????????

                                }


                            },sysHour,sysMinute,false);

                    myPickerDialog.show();


                }



                // Button Events

                counter++;

                if (counter == 1) {



                    //calculate hour
                    if(sysHour > timeHour) {
                        while (timeHour != sysHour) {

                            sysHour++;
                            countHour++;
                            if (sysHour == 12)
                                sysHour = 1;

                        }
                        timeHour = countHour;
                    } else {
                        sysHour = timeHour-sysHour;
                        timeHour = sysHour;
                    }

                    // calculate minute

                    if (timeMinute < sysMinute) {
                        timeMinute = 60 - sysMinute + timeMinute;
                    } else {
                        timeMinute -= sysMinute;
                    }

                    //calculate second

                    if (timeSecond < second) {
                        timeSecond = 60 - second + timeSecond;
                    } else {
                        timeSecond -= second;
                    }


                    //hour to millisecond
                    total = timeHour * 3600000;
                    //minute to millisecond
                    total = total + (timeMinute * 60000);
                    //second to millisecond
                    total = total + (timeSecond * 1000);

                } else {
                    topTxt.setText("MEETING STARTED!");
                    //CountDownTimer
                    new CountDownTimer(total, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            if (stop == true) {
                                cancel(); // If press to stopConverter, stop value will be true and countdown will be canceled
                            } else {
                                Downtxt.setText("" + String.format(FORMAT,
                                        TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
                                                TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                                                TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                            }

                        }


                        @Override
                        public void onFinish() {
                            topTxt.setText("Timer finished!");

                        }
                    }.start();

                    // Start Listening

                    counter = 0;

                }

            }
        });

        //Stop Button Onclick Methods

        stopConvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop = Boolean.valueOf("true");
                topTxt.setText("Converting Finished!");
                timeHour = 00;
                timeMinute = 00;
                timeSecond = 00;
                Downtxt.setText(String.format(FORMAT, timeHour, timeMinute, timeSecond));
                counter = 0;
                //Stop Listening

            }
        });


    }



    //Record method


    //Voice permission

}

0 ответов

Другие вопросы по тегам