Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Figured out a work-around, in setting the default ringtone.</p> <p>For the people who uses both a <code>RingtonePreference</code> and <code>PreferenceManager.setDefaultValues()</code>, <code>android:defaultValue</code> on a <code>RingtonePreference</code> takes in a string to a ringtone's URI. By providing an empty string, you're defaulting the preference to "silence," while other strings will probably lead to no valid URI.</p> <p>The work-around, then, is to provide a bogus string, such as <code>android:defaultValue="defaultRingtone"</code>:</p> <pre><code>&lt;RingtonePreference android:key="alarm" android:title="Alarm" android:name="Alarm" android:summary="Select an alarm" android:ringtoneType="alarm" android:showDefault="true" android:defaultValue="defaultRingtone" /&gt; </code></pre> <p>When calling <code>PreferenceManager.setDefaultValues()</code>, grab the preference, and check if the bogus string is being stored:</p> <pre><code>// Set the stored preferences to default values defined in options.xml PreferenceManager.setDefaultValues(this, R.layout.options, false); // Check the stored string value, under the RingtonPreference tag final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); final String savedUri = savedState.getString("alarm", ""); // By default, set the alarm's URI to null Uri alarmUri = null; // Check if a String was actually provided if(savedUri.length() &gt; 0) { // If the stored string is the bogus string... if(savedUri.equals("defaultRingtone")) { // Set the alarm to this system's default alarm. alarmUri = Settings.System.DEFAULT_ALARM_ALERT_URI; // Save this alarm's string, so that we don't have to go through this again final SharedPreferences.Editor saveEditor = saveState.edit(); saveEditor.putString("alarm", alarmUri.toString()); saveEditor.commit(); } // Otherwise, retrieve the URI as normal. else { alarmUri = Uri.parse(savedUri); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload