从 android 首选项设置铃声无效

Setting Ringtone from android preferences is not working

我有一个提醒应用程序,它允许用户从我使用 android 首选项实现的设置选项设置通知所需的铃声。

铃声对话框与列表一起显示,我 select 的铃声已更新,但 selected 铃声未设置为通知。它始终使用设备的默认铃声进行通知。

我参考并尝试了类似问题的答案,但它不起作用

Preferences.xml

<?xml version="1.0" encoding="utf-8"?>
  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <PreferenceCategory
            android:title="Settings">
    <RingtonePreference
            android:name="RingtonePreference"
            android:summary="Select a ringtone"
            android:title="Ringtones"
            android:key="@string/ringtonePref"
            android:defaultValue="content://settings/system/notification_sound"
    />
</PreferenceCategory>
</PreferenceScreen>

Prefrence.java

public class Preferences extends PreferenceActivity implements OnPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    String ringtonePreference;
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    ringtonePreference = prefs.getString("ringtonePref",
            "DEFAULT_RINGTONE_URI");
}
@Override
protected void onResume() {
    super.onResume();
    RingtonePreference pref = (RingtonePreference) findPreference(getString(R.string.ringtonePref));
    pref.setOnPreferenceChangeListener(this);

}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    // TODO Auto-generated method stub
    updateRingtoneSummary((RingtonePreference) preference, Uri.parse((String) newValue));
    return true;
}

private void updateRingtoneSummary(RingtonePreference preference, Uri ringtoneuri) {
    // TODO Auto-generated method stub
    Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneuri);
    if (ringtone != null)
        preference.setSummary(ringtone.getTitle(this));
    else
        preference.setSummary("Silent");
}

}

MainActivity.java

    @Override
     public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {

    case R.id.menu_settings: 
        Intent intent = new Intent(this, Preferences.class); 
        startActivity(intent); 
        return true;
    case R.id.menu_about: 
        Intent i = new Intent(this, About.class); 
        startActivity(i); 
    }

    return super.onMenuItemSelected(featureId, item);
}

NotificationService.java

NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(R.drawable.ic_icon, title, System.currentTimeMillis());
    note.setLatestEventInfo(this, title, "You have a task to be done!", pi);
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int)((long)rowId);
    mgr.notify(id, note); 

步骤 1) 从首选项中获取铃声。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// DO NOT ever call getBaseContext() unless you know what you're doing. "this" or "getApplicationContext() is perfect. Google the difference.
String ringtonePreference = prefs.getString("ringtonePref", "DEFAULT_RINGTONE_URI");
// The key of preference was "@string/ringtonePref" which is useless since you're hardcoding the string here anyway.
Uri ringtoneuri = Uri.parse(ringtonePreference);

考虑使用 DEFAULT_NOTIFICATION_URI 而不是 DEFAULT_RINGTONE_URI

第2步)设置为通知。

NotificationCompat.Builder b = new NotificationCompat.Builder(context)
    .setSound(ringtoneuri)
    ...