2013-07-12 241 views
6

這是一個倒計時計時器,我想在幾分鐘和幾秒鐘內打印。 EX(300000mili)5分鐘,算上4:60,4:59 ... 這裏是我的代碼在幾分鐘和幾秒鐘內倒計時

 final MyCounter timer = new MyCounter(300000,1000); 
    blue.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      timer.start(); 
     } 
    }); 
} 
public class MyCounter extends CountDownTimer{ 

    public MyCounter(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
     blue.setText("Live"); 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     blue.setText((millisUntilFinished/60000)+":"+(millisUntilFinished/5000)); 
     // i tried this 
    } 
} 

,我怎麼可以把振動部分缺失20秒什麼時候?什麼時候結束? 感謝和抱歉我的英文不好

+0

問題在哪裏? – amalBit

+0

現在是/現在不行嗎? – codeMagic

回答

36

http://developer.android.com/reference/android/os/CountDownTimer.html

TextView _tv = (TextView) findViewById(R.id.textView1); 
new CountDownTimer(30000, 1000) { // adjust the milli seconds here 

    public void onTick(long millisUntilFinished) { 
    _tv.setText(""+String.format("%d min, %d sec", 
        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); 
    } 

    public void onFinish() { 
     _tv.setText("done!"); 
    } 
}.start(); 

由於Java 1.5存在java.util.concurrent.TimeUnit中類

_tv.setText(""+String.format("%d min, %d sec", 
        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); 

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html

TimeUnit表示在給定的單位時間段的粒度,並提供實用的方法來轉換acr oss單元,並在這些單元中執行計時和延遲操作。

對於Java版本低於1.5或者不完全支持TIMEUNIT類下面的公式可用於系統:

int seconds = (int) (milliseconds/1000) % 60 ; 
int minutes = (int) ((milliseconds/(1000*60)) % 60); 
int hours = (int) ((milliseconds/(1000*60*60)) % 24); 

振動

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
      // Vibrate for 500 milliseconds 
      v.vibrate(500); 
    <uses-permission android:name="android.permission.VIBRATE"/> // permission in manifest 

編輯:

震動在4分55秒500毫秒

if((TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)==4) && 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))==55) 
      { 

      Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
      // Vibrate for 500 milliseconds 
      v.vibrate(500); 
      } 
+0

如何在丟失20秒時振動?什麼時候結束? thx –

+0

@LucaPimenta你需要找到一種方法來比較和使用編輯的答案中的代碼 – Raghunandan

+0

@LucaPimenta檢查編輯 – Raghunandan

2

只需使用模運算符%即可獲取剩餘的毫秒數併除以1000轉換爲秒。

blue.setText((millisUntilFinished/60000)+":"+(millisUntilFinished % 60000/1000)); 
+0

工作thx男人! –

2
package com.zt.voicerecorder.controller; 

import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.TextView; 
import com.zt.voicerecorder.R; 

public class MainActivity extends AppCompatActivity { 

    private TextView tvTimer; 
    private Handler handler = new Handler(); 
    private Runnable runnable; 
    private long milliSeconds = 0; 

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

     tvTimer = (TextView) findViewById(R.id.tvTimer); 
     timerStart(); 

    } 

    private void timerStart() { 
     runnable = new Runnable() { 
      @Override 
      public void run() { 
       handler.postDelayed(this, 1000); 
       milliSeconds = milliSeconds + 1000; 

       int hours = (int) ((milliSeconds/(1000 * 60 * 60)) % 24); 
       int minutes = (int) ((milliSeconds/(1000 * 60)) % 60); 
       int seconds = (int) (milliSeconds/1000) % 60; 

       tvTimer.setText(String.format("%02d", hours) 
         + ":" + String.format("%02d", minutes) 
         + ":" + String.format("%02d", seconds)); 
      } 
     }; 
     handler.postDelayed(runnable, 0); 
    } 

    @Override 
    protected void onDestroy() { 
     handler.removeCallbacks(runnable); 
     super.onDestroy(); 
    } 
} 
相關問題