1 package com.android.customization.picker; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.IntentFilter; 7 8 import androidx.annotation.Nullable; 9 10 /** 11 * BroadcastReceiver that can notify a listener when the system time (minutes) changes. 12 * Use {@link #registerNewReceiver(Context, TimeListener)} to create a new instance that will be 13 * automatically registered using the given Context. 14 */ 15 public class TimeTicker extends BroadcastReceiver { 16 17 public interface TimeListener { onCurrentTimeChanged()18 void onCurrentTimeChanged(); 19 } 20 registerNewReceiver(Context context, TimeListener listener)21 public static TimeTicker registerNewReceiver(Context context, TimeListener listener) { 22 TimeTicker receiver = new TimeTicker(listener); 23 // Register broadcast receiver for time tick 24 final IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); 25 context.registerReceiver(receiver, filter); 26 return receiver; 27 } 28 29 @Nullable private TimeListener mListener; 30 TimeTicker(TimeListener listener)31 private TimeTicker(TimeListener listener) { 32 mListener = listener; 33 } 34 35 @Override onReceive(Context context, Intent intent)36 public void onReceive(Context context, Intent intent) { 37 if (mListener != null) { 38 mListener.onCurrentTimeChanged(); 39 } 40 } 41 } 42