Android Date Changed Broadcast Receiver

混江龙づ霸主 提交于 2019-12-11 13:25:16

问题


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


来源:https://stackoverflow.com/questions/24134512/android-date-changed-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!