Listen for Time Zone Changes Even When the App is Closed

Viewed 236

I am trying to listen for timezone changes even when my Android app is closed.

What I tried:

  1. I found a intent action for it. It is TIME_ZONE_CHANGED. However it is a protected intent that can only sent by system as the documentation says and also it probably doesn't allow to make it implicit broadcast.

  2. I tried AlarmManager, but I couldn't find exact timezone change events.

  3. I used schedululeAtFixedRate in a thread in an app service. It worked perfectly. But I don't want it to listen to every hour changes, I want only time zone timezone changes, as I mentioned above.


Edit:

MainActivity

public static final String CHANNEL_ID = "49";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createNotificationChannel();
    startService(new Intent(this,AppService.class));
}

 private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence c_name = getString(R.string.notification_channel_name);
        String c_desc = getString(R.string.notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,c_name,importance);
        notificationChannel.setDescription(c_desc);
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(notificationChannel);
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    <receiver
        android:name=".TimeChangedReceiver"
        android:enabled="true"
        android:exported="true">

        <intent-filter >
            <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
        </intent-filter>

    </receiver>

    <service
        android:name=".AppService"
        android:enabled="true"
        android:exported="true" />

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Receiver Class

package com.example.gridview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class TimeChangedReceiver extends BroadcastReceiver {

private static int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    StringBuilder sb = new StringBuilder("Timezone is changed 
    YAY!,executed for "+NOTIFICATION_ID+" times.");
    if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
        sb.append("\n Status: Ok.");
    }
  
    NotificationCompat.Builder builder = new 
    NotificationCompat.Builder(context,MainActivity.CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentText(sb.toString())
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Hey!");
    NotificationManagerCompat notificationManager = 
    NotificationManagerCompat.from(context);
    notificationManager.notify(NOTIFICATION_ID,builder.build());
    NOTIFICATION_ID++;
}

}

1 Answers

You should register a manifest receiver for "android.intent.action.TIMEZONE_CHANGED" (AKA Intent.ACTION_TIMEZONE_CHANGED). It is one of the implicit broadcast exceptions that is not subject to the restrictions on registering manifest receivers in Android 8.0+.

A manifest receiver like this is going to be the only way to detect this type of event when your app is not running.

Related