Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set RingtonePreference value from code?

I've the following Preference:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
   <RingtonePreference
    android:showDefault="true"
    android:showSilent="true"
    android:title="@string/feed_alert_ringtone"
    android:ringtoneType="ringtone|notification|alarm|all"
    android:key="alertringtone"
    android:persistent="false">
   </RingtonePreference>     
</PreferenceScreen>

When a user changes this preference, I manually save the Uri to a database:

public boolean onPreferenceChange(Preference pref, Object change) {
    String ringtone = change.toString();

    // save it to a db
    ...

    return true;
}

My problem is, when the user closes and then gets back to the PreferenceScreen, the default Value of the RingtonePreference is always Silence. Of course, I have to set the value manually.

Tried this one in my PreferenceActivity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings); // the XML above

    String database_ringtone = ... // get the string saved above from db
                                   // and according to some parameters passed
                                   // to the intent

    Preference ringtone = findPreference("alertringtone");
    ringtone.setDefaultValue( database_ringtone );

    // also tried:
    ringtone.setDefaultValue( Uri.parse(database_ringtone) );
}

Unfortunatly the preference stays at "Silence" (which means empty). I thought, that when the Preference is not persistent the default value will be used.

I cannot make it persistent, because the preference is used for multiple items and the data I get from DB changes according to some extra data passed to the intent.

Nevertheless, I do not want to set the default value, but the current value I get from database. Thought this might be a workaround. So any other way would be good, too.

Double-checked saving and retrieving from DB is working, so that's not the problem.

How can I set the RingtonePreference value from code?


Edit: Here's a little explanation what I want to do, maybe there's another way.

I've several menu entries (count varies from user to user), for which the user can select a ringtone for. Selecting the ringtone works with the xml above, the ID of the menu entry is passed with an Intent to the PreferenceActivity. The URI of the ringtone is then stored in the database.

When one of the entries changes, an alarm is played according to the ringtone-selection the user made. So he knows which one changed. No problem so far.

The problem is, when the user wants to change the ringtone for an entry he already defined one for. If the user selected ringtone Foo before, when clicking on the RingtonePreference, the ringtone Foo should be preselected already. This is no serious error (more like a glitch), but still very annoying.

As far as I can tell, there is no way to preselect a default RingtonePreference from code? Or am I doing it "just wrong"?


Edit 2: Okey, I think there isn't a way to do this. Very weird, the android core. Allows us to use a Preference and get values from it but not to put values back in. Must have been a real hurry. I'm glad they allowed us to check a CheckBoxPreference from code...

like image 848
Markus Avatar asked May 19 '11 16:05

Markus


1 Answers

I was searching how to set default value for the ringtone, and realized the same thing as you, that when the preference is not set than the value is empty and the silent is selected as default. But I do this

// I read my ringtone setting (I read the value from my ringtone_uri key)
// then if it is not set I set the value with the default value from the phone
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Uri configuredUri = Uri.parse(sharedPrefs.getString("ringtone_uri", Settings.System.DEFAULT_RINGTONE_URI.toString()));

// then I do this, I save the default ringtone to my settins
if(configuredUri.equals(Settings.System.DEFAULT_RINGTONE_URI)){
    sharedPrefs.edit()
            .putString("ringtone_uri", Settings.System.DEFAULT_RINGTONE_URI.toString())
            .commit();
}

I do not know if this will help you but I hope it will help to someone else. btw I freak out finding this workaround

like image 62
Lukap Avatar answered Oct 09 '22 00:10

Lukap