2011-06-29 35 views
2

我一直在試圖爲Android寫一個簡單的數字時鐘應用程序。首先,我使用內置的數字時鐘,並試圖省略秒顯示,但我不能。我想要時鐘的秒數來觸發我的一些代碼行。如何在Android上創建數字時鐘?

然後,我開始開發簡單的應用程序,將數字放在屏幕上,但我不知道如何以最有效的方式觸發顯示。任何人都可以給我一個簡單的開始,在哪裏看,以及使用哪些功能?我如何在每秒內執行幾行代碼?我嘗試了Display the current time and date in an Android application的例子,但沒有成功。沒有名爲CountDownRunner的庫可以導入。

我是android開發新手,但是我在Java,C,C++方面有相當不錯的經驗。

謝謝

+0

數字時鐘上述API級別17的折舊建議您使用TextClock,這裏是它的支持庫這對我來說工作https://開頭github.com/vojtech/android-textclock-backport/branches –

回答

0

看一看爲DigitalClock插件的源代碼。特別是看看onAttachedToWindow()方法,該方法創建一個Runnable來處理1秒鐘的「滴答聲」。

8

這裏是一個簡單的時鐘,我使用Android本機DigitalClock.java和AnalogClock.Java類中使用的方法開發。 此時鐘支持每秒或每分鐘觸發的事件,因此您可以使用這些事件並運行您的代碼。

  1. StartTickPerSecond()方法使用在DigitalClock.java用於獲取每秒蜱的方法。
  2. StartTickPerMinute()方法使用AnalogClock.java使用的方法來獲取每分鐘節拍

,這仍然可能需要一些modifications..but希望這將是你的東西入手..

Clock.java

package com.sample.data; 

import java.util.*; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.text.format.Time; 
public class Clock 
{ 
    public static final int TICKPERSECOND=0; 
    public static final int TICKPERMINUTE=1; 

    private Time Time; 
    private TimeZone TimeZone; 
    private Handler Handler; 
    private List<OnClockTickListner> OnClockTickListenerList = new ArrayList<OnClockTickListner>(); 

    private Runnable Ticker; 

    private BroadcastReceiver IntentReceiver; 
    private IntentFilter IntentFilter; 

    private int TickMethod=0; 
    Context Context; 

    public Clock(Context context) 
    { 
     this(context, Clock.TICKPERMINUTE); 
    } 
    public Clock(Context context,int tickMethod) 
    { 
     this.Context=context; 
     this.TickMethod=tickMethod; 
      this.Time=new Time(); 
     this.Time.setToNow(); 

     switch (TickMethod) 
     { 
      case 0: 
       this.StartTickPerSecond(); 
       break; 
      case 1: 
       this.StartTickPerMinute(); 
       break; 

      default: 
       break; 
     } 
    } 
    private void Tick(long tickInMillis) 
    { 
     Clock.this.Time.set(Clock.this.Time.toMillis(true)+tickInMillis); 
     this.NotifyOnTickListners(); 
    } 
    private void NotifyOnTickListners() 
    { 
     switch (TickMethod) 
     { 
      case 0: 
       for(OnClockTickListner listner:OnClockTickListenerList) 
       { 
        listner.OnSecondTick(Time); 
       } 
       break; 
      case 1: 
       for(OnClockTickListner listner:OnClockTickListenerList) 
       { 
        listner.OnMinuteTick(Time); 
       } 
       break; 
     } 

    } 
    private void StartTickPerSecond() 
    { 
     this.Handler=new Handler(); 
     this.Ticker = new Runnable() 
     { 
      public void run() 
      {   
       Tick(1000); 
       long now = SystemClock.uptimeMillis(); 
       long next = now + (1000 - now % 1000);   
       Handler.postAtTime(Ticker, next);    
      } 
     }; 
     this.Ticker.run(); 

    } 
    private void StartTickPerMinute() 
    { 
     this.IntentReceiver= new BroadcastReceiver() 
     { 
      @Override 
      public void onReceive(Context context, Intent intent) 
      { 
       Tick(60000); 

      } 
     }; 
     this.IntentFilter = new IntentFilter(); 
     this.IntentFilter.addAction(Intent.ACTION_TIME_TICK); 
     this.Context.registerReceiver(this.IntentReceiver, this.IntentFilter, null, this.Handler); 

    } 
    public void StopTick() 
    { 
     if(this.IntentReceiver!=null) 
     { 
      this.Context.unregisterReceiver(this.IntentReceiver); 
     } 
     if(this.Handler!=null) 
     { 
      this.Handler.removeCallbacks(this.Ticker); 
     } 
    } 
    public Time GetCurrentTime() 
    { 
     return this.Time; 
    } 
    public void AddClockTickListner(OnClockTickListner listner) 
    { 
     this.OnClockTickListenerList.add(listner); 

    } 

} 

OnClockTickListne r.java

public interface OnClockTickListner { 
    public void OnSecondTick(Time currentTime); 
    public void OnMinuteTick(Time currentTime); 
} 

如何使用

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.clock_list_item); 
     Clock c=new Clock(this); 
     c.AddClockTickListner(new OnClockTickListner() { 

      @Override 
      public void OnSecondTick(Time currentTime) { 
       Log.d("Tick Test per Second",DateFormat.format("h:mm:ss aa ", currentTime.toMillis(true)).toString()); 

      } 

      @Override 
      public void OnMinuteTick(Time currentTime) { 
       Log.d("Tick Test per Minute",DateFormat.format("h:mm aa", currentTime.toMillis(true)).toString()); 

      } 
     }); 

    } 


}