I have an android application. Here whenever the user changes the device timezone, I want a event listener to notify me? How can I accomplish it?
Show activity on this post. Yes, there is. The ACTION_TIME_CHANGED Intent is broadcast when the device time is changed, and you can have a method which will trigger when this Intent is detected. This intent has been in Android since API level 1, so it should work on any platform you might need to be compatible with.
The Time Zone Data module updates daylight saving time (DST) and time zones on Android devices, standardizing both the data (which can change frequently in response to religious, political, and geopolitical reasons) and the update mechanism across the ecosystem.
Location Time Zone Detection, available on Android 12 or higher, is an optional automatic time zone detection feature that allows devices to use their location and time zone map data to determine the time zone. Location time zone detection is an alternative mechanism to telephony time zone detection.
Register a receiver with the following intent filters
filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_TIME_CHANGED);
and register it in the OnCreate
of your activity
public void onCreate() {
super.onCreate();
registerReceiver(receiver, filter);
}
Create a Broadcast receiver as follows.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED))
{
doWorkSon();
}
}
};
and unregister it in the onDestroy
of your activity:
public void onDestroy() {
super.onDestroy();
unregisterReceiver(m_timeChangedReceiver);
}
You need to create a BroadcastReceiver for intent action ACTION_TIMEZONE_CHANGED. BroadcastReceiver is described here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With