1

I have a preference for an HTTP timeout set in milliseconds on the backend. But when the user edits it, I want them to be able to specify the number in minutes.

I have looked at this (SharedPreferences.onSharedPreferenceChangeListener not being called consistently) and it looks like it is possible to add a listener to see when a preference value changes. So I could convert the minutes back into milliseconds.

But how can I, when the preference activity loads, convert the milliseconds in to minutes so that when the user goes to edit they see the minutes value?

Community
  • 1
  • 1
b85411
  • 9,420
  • 15
  • 65
  • 119

1 Answers1

0

Hi as you need to convert milliseconds to minutes and second combination so for this purpose you can use following code snippet

long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

or if you wants to go with generic way than

seconds = (millis/ 1000) % 60;
minutes = (millis/ (1000 * 60)) % 60;
hours = (millis/ (1000 * 60 * 60)) % 24;

note millis means milliseconds

and in a reverce way you can convert minutes to millis

long t = (min * 60L) + sec;

long result = TimeUnit.SECONDS.toMillis(t);

beside this if you wants to detect if changes were made on the preferences==>

You need to register your listener than register it in onResume and unregister in onPause as follows

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
}

@Override
protected void onResume() {
    super.onResume();
    prefs.registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {      
    super.onPause();
    prefs.unregisterOnSharedPreferenceChangeListener(this);
}

please visit

http://androidpartaker.wordpress.com/2012/02/05/change-preference-summary/

preference activity listener in android

http://developer.android.com/reference/android/preference/Preference.OnPreferenceChangeListener.html

Community
  • 1
  • 1
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
  • Thank you, but my question was more around how can I hook into an event when the preference activity loads such that I could be in a position to do as you suggested. – b85411 Mar 10 '14 at 04:09
  • please visit http://stackoverflow.com/questions/11203576/preference-activity-listener-in-android hope it will help you also visit http://developer.android.com/reference/android/preference/Preference.OnPreferenceChangeListener.html – Jitesh Upadhyay Mar 10 '14 at 04:11
  • Yes, thanks, but as I said in the first post I'm aware I can hook into when the shared preference value changes which is great. But how can I hook into it when it *loads*, such that I can convert the milliseconds into minutes? – b85411 Mar 10 '14 at 04:13
  • did you tried with prefs.registerOnSharedPreferenceChangeListener(this); and prefs.unregisterOnSharedPreferenceChangeListener(this); – Jitesh Upadhyay Mar 10 '14 at 04:19
  • Please let me know if I am mis-understanding your answer, but the SharedPreferenceChangeListener seems to be only useful when *editing* a value. It doesn't appear to get called when *loading* the value. – b85411 Mar 10 '14 at 04:39
  • let me check and will get back to you if i will be having a valid and perfect answer!! i am confused too, yours question is interesting and valid!! – Jitesh Upadhyay Mar 10 '14 at 04:53