RingtonePreference is always null :(
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Notifications">
<CheckBoxPreference
android:key="vibration"
android:title="Vibrate"
android:summary="Vibrate phone for notifications" />
<CheckBoxPreference
开发者_开发知识库 android:key="play_tone"
android:title="Play Ringtone"
android:summary="Play Ringtone for notifications" />
<RingtonePreference
android:key="app_ringtone"
android:dependency="play_tone"
android:title="Select Ringtone"
android:ringtoneType="notification"
android:showDefault="true"
android:shouldDisableView="true"
android:summary="Pick a Ringtone" />
</PreferenceCategory>
</PreferenceScreen>
This is my preference xml. But,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.getString("app_ringtone", null);
always returns null. I am debugging on a device (HTC Wildfire).
In my case problem was that I overrided onActivityResult in PreferenceActivity and didn't invoke super.onActivityResult(...). Now it works fine:
public synchronized void onActivityResult(final int requestCode,
int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Here is a sample project demonstrating collecting preferences, including ringtones. If this does not work on your device, then there may be a compatibility problem with the device.
Problematic XML:
<activity android:name="Activity1"
android:label="Activity 1"/>
<activity android:name="Activity2"
android:label="Activity 2" />
<activity android:name="Settings"
android:label="Settings" />
I changed the android:name="Activity1" to android:name=".Activity1" FOR ALL ACTIVITIES and the code started working. The code also malfunctions if the android:showSilent="false" attribute is included in the RingtonePreference may be this is a bug. I figured this out after a lot of trial and errors. Any ideas, pls enlighten @Commonsware.
Working XML:
<activity android:name=".Activity1"
android:label="Activity 1"/>
<activity android:name=".Activity2"
android:label="Activity 2" />
<activity android:name=".Settings"
android:label="Settings" />
精彩评论