ACTION_TIME_CHANGED or ACTION_DATE_CHANGED, can't make them work

前端 未结 6 1351
小鲜肉
小鲜肉 2021-01-14 12:43

So I am in the process of trying to get a few intents to work for me, and am having problems on 2 specific intents. I searched these groups and the webs and can \'t seem

相关标签:
6条回答
  • 2021-01-14 13:00

    Intent.ACTION_DATE_CHANGED and Intent.ACTION_TIME_CHANGED are triggered only when the user manually changes time or date, and they are not automatically triggered by the system.

    I fear you have to use Intent.ACTION_TIME_TICK, which is triggered by the system exactly each minute.

    0 讨论(0)
  • 2021-01-14 13:01

    In addition to the <intent-filter>, see Android ACTION_DATE_CHANGED broadcast about an AOSP bug: ACTION_TIME_CHANGED should fire when the clock gets set and ACTION_DATE_CHANGED should fire when the clock ticks over to the next day, but if the clock gets set backwards, ACTION_DATE_CHANGED won't fire again until the clock catches up with what was going to be the next day.

    0 讨论(0)
  • 2021-01-14 13:07

    An alternative approach which I used to take an action when the date is changed.

    Actually, the Intent.ACTION_DATE_CHANGED only works with the date of an advanced date and not with a previous date when you change the date on the phone manually. Also, when you set time to 11:59 and wait for it to become 00:00 to give you the broadcast event of the date change, it doesn't give you the trigger.

    So, I ended up making my own logic of storing a date in shared pref and then checking if the current date is after or before previously stored date (with some date processing logic) and updating my data based on that.

      private void takesomeActionIfDateIsChanged() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    
            long currentDateTimeInMillis = System.currentTimeMillis();
            Date currentDateWithMillis = new Date(currentDateTimeInMillis);
    
            String formattedCurrentDateWithOnlyDMYStr = dateFormat.format(currentDateWithMillis);
            Log.d(TAG, "formattedCurrentDate With Only DateMonthAndYear String : " + formattedCurrentDateWithOnlyDMYStr);
            Date formattedCurrentDate = dateFormat.parse(formattedCurrentDateWithOnlyDMYStr);
    
            String storedDateStr = mSpManager.getStoredDate(SPManager.STORED_DATE);
            Log.d(TAG, "storedDateStr : " + storedDateStr);
            Date storedDateOnlyWithDateMonthAndYear = dateFormat.parse(storedDateStr);
    
            if (formattedCurrentDate.after(storedDateOnlyWithDateMonthAndYear)) {
                Log.d(TAG, "The current date is after stored date");
                mSpManager.putNewDate(SPManager.STORED_DATE, formattedCurrentDateWithOnlyDMYStr);
                mCounter = 0;
            } else if (formattedCurrentDate.before(storedDateOnlyWithDateMonthAndYear)) {
                Log.d(TAG, "The current date is before stored date");
                mSpManager.putNewDate(SPManager.STORED_DATE, formattedCurrentDateWithOnlyDMYStr);
                mCounter = 0;
            } else {
                Log.d(TAG, "The current date is same as stored date");
            }
        } catch (Exception e) {
            Log.e(TAG, "exception : ", e);
        }
    }
    
    0 讨论(0)
  • 2021-01-14 13:13

    Yes, you must add

    <intent-filter>
       <action android:name="android.intent.action.DATE_CHANGED"></action>
    </intent-filter>
    
    0 讨论(0)
  • 2021-01-14 13:19

    Inside your broadcast receiver onReceive method I am assuming u have a similar setup to the following?

    @Override
    public void onReceive(Context context, Intent intent){
        final String action = intent.getAction();
        if (Intent.ACTION_DATE_CHANGED.equals(action) || Intent.ACTION_TIME_CHANGED.equals(action)){
        // do stuff here
        }
        ...
    }
    

    Another consideration would be did you add the proper filters to your IntentFilter such as...

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_TIME_CHANGED);
    filter.addAction(Intent.ACTION_DATE_CHANGED);
    
    0 讨论(0)
  • 2021-01-14 13:22

    You don't need any permission.

    In order to be notified of date changes, you need to register to a BroadcastReceiver with those actions (and also ACTION_TIMEZONE_CHANGED) .

    Here's a solution I've made:

    https://stackoverflow.com/a/48782963/878126

    0 讨论(0)
提交回复
热议问题