如何在运行时获取铃声首选项?

时间:2011-02-12 11:45:15

标签: android preferences android-preferences ringtone

我有以下xml:

 <?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Ringtone preference" android:key="ringtone_option_preference">
    <RingtonePreference 
    android:key="ring_tone_pref"
    android:title="Set Ringtone Preference"
    android:showSilent="true"
    android:ringtoneType="notification"
    android:summary="Set Ringtone"/>
 </PreferenceScreen>

我希望每次通知即将显示时,要查看铃声的值并相应地发出蜂鸣声:)...更准确地说,我的通知是在broadcastReceiver类中生成的,每次接收器捕获的东西它会创建一个新的通知...我只是希望通知的铃声根据首选项中设置的铃声进行更改。

我该怎么做?

由于

麦克

2 个答案:

答案 0 :(得分:24)

没关系我找到了它:

 SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
 String strRingtonePreference = preference.getString("ring_tone_pref", "DEFAULT_SOUND");        
 notification.sound = Uri.parse(strRingtonePreference);

答案 1 :(得分:4)

<RingtonePreference
            android:defaultValue="true"
            android:key="ringtone_sound"
            android:ringtoneType="notification"
            android:showSilent="true"
            android:showDefault="true"
            android:title="Sound"
            android:enabled="true" />

然后

String PREFERENCE_SOUND = "ringtone_sound";

private SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

String strRingtonePreference = sharedPreferences.getString(PREFERENCE_SOUND, "DEFAULT_SOUND");
Uri defaultSoundUri = Uri.parse(strRingtonePreference);
notificationBuilder.setSound(defaultSoundUri);