Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an android event listener for change in the timezone of the device?

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?

like image 488
user4501328 Avatar asked Jul 14 '15 05:07

user4501328


People also ask

Is there a way to detect when user has changed the clock time on their device?

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.

What is the time zone data on an Android phone?

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.

What is device time zone?

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.


2 Answers

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);     
}
like image 123
Kartheek Avatar answered Sep 21 '22 03:09

Kartheek


You need to create a BroadcastReceiver for intent action ACTION_TIMEZONE_CHANGED. BroadcastReceiver is described here.

like image 23
Bob Snyder Avatar answered Sep 24 '22 03:09

Bob Snyder