简体   繁体   中英

Odd values with timepicker and SystemClock.elapsedRealtime();

I have a TimePicker in my app that can select the amount of time for playback of a song, im having issues with getting the time remaining to display properly with the CountDownTimer im using, everything is pretty much going on behind the scenes in milliseconds of course and i think im doing the conversion right but all i see on the screen is just completely wrong values.

When i just make a test CountDownTimer with 3600000(1hour) as first argument, everything works fine, but when i put timepicker to 0 and minute to 1 , like i want just 1 minute of playback , it displays 12 hours and what seems like random values in the minutes and seconds slots. tp.getCurrentHour(); returns zero when set to zero and tp.getCurrentMinute(); returns one as expected, seems like something is happening with this part, cant figure out what yet:

playtime = (hour * (60 * 60 * 1000)) + (min * (60 * 1000)); startime = SystemClock.elapsedRealtime();

Why is tv2.setText("totaltest "+startime+playtime); displaying a value of 6000046929803??? That is obviously wrong...

Here is the the rest of the code:

                TimePicker tp =(TimePicker)findViewById(R.id.timePicker1);

                    public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    long hour = tp.getCurrentHour();
                                    long min = tp.getCurrentMinute();
                                    playtime = (hour * (60 * 60 * 1000)) + (min * (60 * 1000));
                                    startime = SystemClock.elapsedRealtime();
tv2.setText("totaltest "+startime+playtime);
            timer = new CountDownTimer(startime+playtime,1000){

                                @Override
                                public void onFinish() {
                                    tv.setText("finished");
                                }

                                //@SuppressLint("ParserError")
                                @Override
                                public void onTick(long millisUntilFinished) {

                                    String display = DateUtils.formatElapsedTime(millisUntilFinished/1000);

                                    tv.setText(display);

                                }

                            }.start();

Try:

String display = DateUtils.formatElapsedTime(millisUntilFinished/1000);

That does the conversion for you.

and:

timer = new CountDownTimer(playtime,1000){ 

as CounterDownTimer is expecting the amount of milliseconds to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM