Search code examples
androiddateandroid-intentbroadcastreceiverandroid-broadcast

Android: is android.intent.action.DATE_CHANGED triggered at device reboot?


I made this broadcast receiver

public class DateReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
     Log.i("DATE RECEIVER", "OK");
   }
}

registered in manifest

     <receiver
        android:name=".DateReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
     </receiver>

It works when device is on but if I turn it off and wait for midnight to pass, then I don't get any intent at reboot. How should I get it?


Solution

  • You won't get this broadcast because date has NOT changed (unless i.e. there's time update after the boot). It is different but has not changed in the way that justifies this broadcast. This may be confusing but in fact it does not matter what time stamp is when device starts. As device was started it does not know if that was because of restart or it was off for 5 weeks. Broadcast will be send if time is artificially changed i.e. due to network time sync, manual time change via preferences, timezone change. Normal ticking does not count. Initial time stamp does not matter.

    If you need to know date on boot, you should listen to BOOT_COMPLETED.

    You also need to remove android:exported="false" or set it to true as otherwise it is not reachable.