5

我有以下 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>

我希望每次通知即将显示时,查看铃声的值并相应地发出哔哔声:)...更准确地说,我的通知是在广播接收器类中生成的,并且每次接收器捕捉到它创建的东西时新通知...我只想根据首选项中设置的铃声更改通知的铃声。

我怎样才能做到这一点?

谢谢

麦克风

4

2 回答 2

24

没关系,我找到了它:

 SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);
 String strRingtonePreference = preference.getString("ring_tone_pref", "DEFAULT_SOUND");        
 notification.sound = Uri.parse(strRingtonePreference);
于 2011-02-12T12:31:12.793 回答
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);
于 2016-05-17T13:30:26.937 回答