0

i tried below code. But i'm not able to read the ring tone from ringtone preferences.

        <RingtonePreference 
            android:title="@string/RING_TONE_SETTING" 
            android:summary="@string/RING_TONE_SETTING_DESCRIPTION" 
            android:ringtoneType="notification"
            android:showSilent="true"
            android:showDefault="true"
            android:key="prefnotificationTone"/>

This is how i'm reading,

 SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
    String strRingtonePreference = preference.getString("prefnotificationTone", "DEFAULT_SOUND");        

    Uri notification = Uri.parse(strRingtonePreference);

    mMPlayer = MediaPlayer.create(getApplicationContext(), notification);
    mMPlayer.start();

I'm getting DEFAULT_SOUND always.

Naruto
  • 9,476
  • 37
  • 118
  • 201

2 Answers2

0

Add the following attribute to the preference in XML:

android:persistent="true"
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Is the preference activity / fragment also using the default shared preferences? – Eugen Pechanec Jan 02 '15 at 05:53
  • Yes, and in that fragment do you have a line similar to this? `getPreferenceManager().setSharedPreferencesName("something");` – Eugen Pechanec Jan 02 '15 at 11:39
  • i'm using third party lib https://github.com/kolavar/android-support-v4-preferencefragment – Naruto Jan 02 '15 at 13:40
  • I've used that too but it shouldn't affect anything. Try this: Open `Android Device Monitor` from Eclipse or Android Studio. Open tab `File Explorer` and navigate to `/data/data//shared_prefs`. There you'll see all preference files used by your app. Tell me what files do you see. I suspect you write and read from different files. – Eugen Pechanec Jan 02 '15 at 17:23
  • Thanks for your help, i did like https://github.com/kolavar/android-support-v4-preferencefragment/issues/1. it worked thank you – Naruto Jan 02 '15 at 18:15
  • You're welcome. I suggest you post your solution as an answer and accept it so others can find it easily. – Eugen Pechanec Jan 02 '15 at 18:21
  • Yes i'll do it, ill update my solution, do you know any answer about this question i.e http://stackoverflow.com/questions/27746707/enable-vibrate-mode-in-android-apps – Naruto Jan 02 '15 at 18:25
0
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
String strRingtonePreference = preference.getString("ring_tone_pref", "DEFAULT_SOUND");        

You just got the uri of the ringtone above. you have to parse it to url and then get the corresponding ringtone from system.

defaultRingtoneUri = Uri.parse(strRingtonePreference);
    defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

Hope this helps.

Abhilash Das
  • 1,388
  • 1
  • 16
  • 22