2012-10-24 40 views
4

我是android新手。我有一個應用程序需要在文本視圖中顯示數據庫中存儲的數據(這是一個時間值,例如40小時)。根據數據庫中的日期在android中設置一個計時器

我想從40(在我的情況下)的值倒數到零,並且一旦發生報警就應該開始爲零。

need to show countdown like image

+0

無法理解你問什麼.. –

+0

[你有什麼嘗試?](http://www.whathaveyoutried.com/) – skunkfrukt

+0

@hardikjoshi。我的應用程序是關於設置一個研究時間,那個時間是在幾小時(如40小時)的形式,這是存儲在數據庫中。我應該在從40開始向下的倒數計時器中在textView中顯示該時間值。當它達到零時,應該開始報警。 – Techy

回答

0

只是轉換在第二剩餘時間(得到分貝小時值,並將其轉換爲第二,在我的情況我收到時間以秒計)

public class CountDownActivity extends Activity { 

    int time,initStart,startPointTime,hh,mm,ss,millis; 
    TextView txtSecond; 
    TextView txtMinute; 
    TextView txtHour; 
    TextView txtDay; 
    Handler handler; 
    long seconds =40*60*60; 
    Runnable updater; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 


    txtSecond=(TextView)findViewById(R.id.cntSecond); 
      txtMinute=(TextView)findViewById(R.id.cntMinute); 
      txtHour=(TextView)findViewById(R.id.cntHour); 
      txtDay=(TextView)findViewById(R.id.cntDay); 

        handler= new Handler(); 
      initStart = (int) SystemClock.elapsedRealtime(); 

      updater = new Runnable() { 

       public void run() { 
        int sec,minute,hour,day; 
        long diff = seconds; 
        System.out.println(diff); 
        if (diff >= 1) { 
         sec = (int) (diff%60); 
        } else { 
         sec=00; 
        } 
        txtSecond.setText("" +sec); 
        diff = diff/60; 
        System.out.println(diff); 
        if (diff >= 1) { 
         minute = (int) (diff%60); 
        } else { 
         minute=00; 
        } 
        txtMinute.setText("" +minute); 
        diff = diff/60; 
        if (diff >= 1) { 
         hour = (int) (diff%24); 
        } else {hour = 00; } 
        txtHour.setText("" +hour); 
        diff = diff/24; 
        if (diff >= 1) { 
         day = (int) diff; 
        } else { day =00; } 
        txtDay.setText("" +day); 
        seconds=seconds-1; 


        handler.postDelayed(this, 1000); 
       } 
      }; 

      handler.post(updater); 
} 

> //and don't forget to removeCallback on destroy 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     handler.removeCallbacks(updater); 
    } 
} 
+0

我可以知道從哪裏可變的秒數得到的值 – Techy

+0

我已經試過你的code.but它不工作 – Techy

+0

請讓我知道你得到什麼?,錯誤或錯誤的結果等,請打印秒值和檢查是否正確或不?如果你得到正確的秒,那麼它應該工作。 – Parker

0

可以使用CountDownTimer爲如下倒計時值:

final MyCounter timer = new MyCounter(600000, 1000); //add your time 

... 
    public class MyCounter extends CountDownTimer 
    { 

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

     @Override 
     public void onFinish() 
     { 
      Log.i("debug","Timer Completed"); 

     } 

     @Override 
     public void onTick(long millisUntilFinished) 
     { 

      tv.setText("Timer : " + (millisUntilFinished/60000) + " " + "minutes remaining."); 
     } 

    } 

對於您可以使用AlarmManager報警部分。同樣的,這裏是一個tutorial

希望它有幫助!