开发者

How to create a Digital Clock on Android?

I've been trying to write a simple Digital Clock app for Android. First I used the built in Digital Clock and tried to omit the seconds display, but I couldn't. I wanted clock's seconds to trigger some lines of code I have.

Then I start to develop simple application to put Digits on scr开发者_运维知识库een but I don't know how to trigger the display in most efficient way. Anybody can give me a simple start, where to look and what functions to use ? How can I execute few lines of code within every second? I tried the example at Display the current time and date in an Android application but it wasn't successful. There were no libraries by the name CountDownRunner to import.

I am new to android development, however I have fairly good experience in Java, C,C++.

Thank you


Here a simple clock i developed using methods used in android native DigitalClock.java and AnalogClock.Java classes.this clock supports for events that triggered for each second or minute,so you can use those events and run your code.

  1. StartTickPerSecond() method uses the method used in DigitalClock.java to get the tick per second.
  2. StartTickPerMinute() method uses the method used in AnalogClock.java to get the tick per minute

this still may require some modifications..but hope this will be something you to start with..

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);

    }

}

OnClockTickListner.java

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

How to use

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());

            }
        });

    }


}


Have a look at the source code for the DigitalClock widget. In particular, look at the onAttachedToWindow() method which creates a Runnable to handle the 1 second 'ticks'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜