Monday, August 1, 2011

How to use ACTION_TIME_TICK broadcast ?

Today, I was looking into how to track change notification to the camera folder in Android. While Googling for this, I came across ACTION_TIME_TICK broadcast event.

Android Doc says
public static final String ACTION_TIME_TICK
The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().

Sometimes we use a backgroud timer in our application to check for something in a continues loop but I think listening to this event is much better approach, but you have to make sure it's thread safe.

How to use.

@Override
public void onCreate(){
super.onCreate();

registerReceiver(
new ThikReceiver(),
new IntentFilter(Intent.ACTION_TIME_TICK));
}
}


public class ThikReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
// Do something
else
// Do something else
}
}

1 comment: