Android Date Changed Broadcast Receiver

2019-08-03 07:45发布

问题:

I have a broadcast receiver that receives event when user manually changes Date . But I am unable to find what was the previous date he changed. Example- If date was 5-June-2014 and he changed it to 01-June-2014. I want to know the previous date that is 5-june-2014. Please help

回答1:

You could register ACTION_TIME_TICK and record the current date every minute.



回答2:

The intent to specify Date Changed is

android.intent.action.DATE_CHANGED

Set a listener to this intent by referring tutorial http://www.tutorialspoint.com/android/android_broadcast_receivers.htm

Create an intent filter :

     static {
        s_intentFilter = new IntentFilter();
        s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
        s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
    }

and a broadcast receiver:

    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();
            }
        }
    };

register the receiver:

     public void onCreate() {
        super.onCreate();
        registerReceiver(m_timeChangedReceiver, s_intentFilter);     
    }

and unregister it:

     public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(m_timeChangedReceiver);     
    }