1

I can't get my BroadcastReciever() to work for DATE_CHANGED, but works just fine for TIMEZONE_CHANGED.

class DateChangeReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Log.d("my broadcast","works")
        if (intent.action == Intent.ACTION_DATE_CHANGED ||
                intent.action == Intent.ACTION_TIMEZONE_CHANGED) {
            var netUtils = NetUtils(context)
            netUtils.mainStack()
        }
    }
}

Here is my Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test">

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/castle"
        android:roundIcon="@drawable/castle"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.VIEW" />

            </intent-filter>
        </activity>
        <receiver android:name=".DateChangeReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DATE_CHANGED"/>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Here is my function in my main activity:

 fun keepAlive() {

    //KEEP RUNNING IN BACKGROUND TO UPDATE WALLPAPER AT CHANGE OF DAY
     val component = ComponentName(this@MainActivity, DateChangeReceiver::class.java)
    packageManager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
     Log.d("KEEPALIVE FUN","works")

 }

keepAlive()

I get nothing in logcat when I change my date manually or if I set the time to 11:59 and wait a minute.

A couple of errors I do have in logcat that show up before I added the Reciever are:

2019-03-05 11:48:01.884 29176-29202/com.test E/libc: Access denied finding property "vendor.debug.egl.profiler"
2019-03-05 11:48:07.616 29176-29234/com.test E/libc: Access denied finding property "ro.vendor.graphics.memory"
2019-03-05 11:48:07.665 29176-29234/com.test E/libc: Access denied finding property "vendor.gralloc.enable_ahardware_buffer"

Is it possible these are effecting my BroadcastReciever? I've tested this on 3 devices so far.

nks
  • 31
  • 3

3 Answers3

1

Solved the issue by switching to the AlarmManager. Here's the code if anyone's curious:

val calendar: Calendar = Calendar.getInstance().apply {
    timeInMillis = System.currentTimeMillis()
    set(Calendar.HOUR_OF_DAY, 3)
}

This method worked better for me as it keeps all devices with my app updating at the same time apposed to start of new day.

val alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val broadcastIntent = Intent(this, DateChangeReceiver::class.java)
val pIntent = PendingIntent.getBroadcast(this,0,broadcastIntent,0)

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.timeInMillis,
        AlarmManager.INTERVAL_DAY,
        pIntent
nks
  • 31
  • 3
1

I registered broacastreceiver for date change implicitly..this worked for me.

registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_DATE_CHANGED));
jyotsna
  • 97
  • 1
  • 9
0

According to this question, there could be a bug. It is obsolete now, but you can try to submit new issue.

Also this question may help you.

They using

ACTION_TIME_TICK

ACTION_TIMEZONE_CHANGED

ACTION_TIME_CHANGED

private final BroadcastReceiver m_timeChangedReceiver = 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();
        }
    }
};
Community
  • 1
  • 1
B-GangsteR
  • 2,534
  • 22
  • 34
  • TICK does every minute and TIME_CHANGED is for manual change of time right? Someone [here](https://stackoverflow.com/questions/7912885/action-time-changed-or-action-date-changed-cant-make-them-work) suggests DATE is depreciated. – nks Mar 05 '19 at 22:38