3

我希望用户能够为我的应用选择通知铃声。我有一个自定义声音文件(一个放在 /raw 中的 .mp3 文件),我想将它作为默认通知铃声。我以这种方式在“共享首选项”中/从“共享首选项”中保存/获取所选铃声 uri:

public static void setNotificationSound(Activity activity, String uri) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
        sharedPref.edit().putString("notificationSound", uri).apply();
    }


    public static Uri getNotificationSound(Context context) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        return Uri.parse(sharedPref.getString("notificationSound", ""));
    }

这就是我显示铃声选择器的方式:

    Uri notificationSoundUri = SharedPrefMethods.getNotificationSound(mACA); //gets the saved uri 

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Notification Ringtone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Uri.parse("android.resource://" + mACA.getPackageName() + "/raw/notificationbrightside")); //my custom sound
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, notificationSoundUri); //pass the saved URI to be 'checked'
    this.startActivityForResult(intent, 555);

我的问题似乎与EXTRA_RINGTONE_EXISTING_URI线路有关。当用户选择系统铃声并返回到选择器时,正确检查了铃声。但是当用户选择“无”或“默认通知声音”(我的自定义声音)并返回选择器时,没有任何检查。

我的代码中的其他所有内容(此处未显示)都可以 - 当我选择任何铃声(包括自定义铃声)时,声音会正确分配给我的通知。当我选择“无”时,不会播放任何声音。这意味着正确的 URI 被保存到 SharedPref。

我必须将什么传递给 Intent,以便在选择“无”或“默认通知声音”时对其进行检查?

4

1 回答 1

1

null选择“无”时通过。

在字段Settings.System.DEFAULT_NOTIFICATION_URI中选择“默认通知声音”时通过。RingtoneManager.EXTRA_RINGTONE_EXISTING_URI

于 2018-01-16T08:45:35.437 回答