Search code examples
androidbroadcastreceiverandroid-2.3-gingerbread

ACTION_TIMEZONE_CHANGED old value?


I would like to have the old value at the Time, date, timezone chnages on Android ( 2.3 ).

I am using a broadcast receiver like:

     <receiver android:name=".broadcastreceiver.OnDateTimeChanged">
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED"/>
            <action android:name="android.intent.action.TIME_SET"/>
            <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
        </intent-filter>
    </receiver>

and inside the java class I would like to have the old value:

public class OnDateTimeChanged extends BroadcastReceiver
{
 @Override
 public void onReceive(Context context, Intent intent)
 {
     // Do some work here
     Log.d("OnDateTimeChanged", "intent action:"+intent.getAction() + ", intent : "+intent);

 }
}

Any idea how to get ( log out ) the OLD value of the time, timezone, date?

Thanks


Solution

  • I don't know any function that gives you the old value, so I suggest you to do this:

    When your app starts get the current values of the time, timezone and date and store it in a variable. You can create a class for it to be simpler. And then store that variable in your dataset to be able to use in any activity (or parse it as extra, as you want). Then everytime the values change just change the variable too. Like:

    public class OnDateTimeChanged extends BroadcastReceiver
    {
     @Override
       public void onReceive(Context context, Intent intent)
       {
          // TimeManager is the name I gave to the class I told you before
          TimeManager timeManager = Provider.dataset().timeManager();
    
          // ... 
    
          // Calculate the elapsed time between the two times, if you need
          // (I didn't test but I think this will work)
          Date oldDateTime = timeManager.getDateTime();
          timeManager.setElapsedTime(currentDateTime - oldDateTime);
    
          // ...
    
          // And then at the end save the current values again,
          // (in this case: the DateTime)
          timeManager.setDateTime(currentDateTime);
       }
    }
    

    Is this what you need? Hope it helps.