Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here a simple clock i developed using methods used in android native DigitalClock.java and AnalogClock.Java classes.<strong>this clock supports for events that triggered for each second or minute</strong>,so you can use those events and run your code.</p> <blockquote> <ol> <li>StartTickPerSecond() method uses the method used in DigitalClock.java to get the tick per second.</li> <li>StartTickPerMinute() method uses the method used in AnalogClock.java to get the tick per minute</li> </ol> </blockquote> <p>this still may require some modifications..but hope this will be something you to start with.. <br> <br> <strong>Clock.java</strong></p> <pre><code>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&lt;OnClockTickListner&gt; OnClockTickListenerList = new ArrayList&lt;OnClockTickListner&gt;(); 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); } } </code></pre> <p><strong>OnClockTickListner.java</strong></p> <pre><code>public interface OnClockTickListner { public void OnSecondTick(Time currentTime); public void OnMinuteTick(Time currentTime); } </code></pre> <p><strong>How to use</strong></p> <pre><code>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()); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload