RingtoneManager显示手机铃声而不是通知声音

时间:2015-06-04 08:16:22

标签: android audio

我使用简单的选择器对话框让用户选择通知声音,这里是启动选择器的代码:

 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM);
 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.selectSound));
 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(LocalCfg.getNotificationSound()));
 startActivityForResult(intent, SELECT_RINGTONE_REQUEST);

LocalCfg.getNotificationSound()只检查SharedPreferences内的设置,并在设置尚不存在的情况下返回默认通知声音Uri

    public static String getNotificationSound() {
    return mPrefs.getString(KEY_PREF_NOTIFY_SOUND_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString());
}
在所有测试的手机上观察到

问题:"默认"列出的声音不是通知/警报声,而是实际的电话铃声(系统默认或用户设置的自定义)。

部分手机(三星Galaxy Young,Xperia Z1 Compact)将其显示为"默认通知声音" (这实际上是错误的),其他一些(Nexus设备,SDK 22)为"默认铃声"。

如果我明确传递RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM标志,为什么会发生?

2 个答案:

答案 0 :(得分:1)

使用RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI extra:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.selectSound));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(LocalCfg.getNotificationSound()));
startActivityForResult(intent, SELECT_RINGTONE_REQUEST);

答案 1 :(得分:0)

我已经看过不同的方法,因为我的手机上有这个问题,Whatsapp似乎自己绕过它(在我的手机上播放自己的音调)。

我研究的唯一可行方法是检查长度(see this answer)并在文件不可用时播放自己的语气,这是我的代码:

//Create default notification ringtone
MediaPlayer mp = MediaPlayer.create(LinxaleApplication.getApplicationInstance(),
        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

//NOTE: not checking if the sound is 0 length, becuase then it should just not be played
if (mp.getDuration() > 5000 /* Ringtone, not notification, happens on HTC 1 m7 5.X version */) {
    mp.release();
    mp = MediaPlayer.create(LinxaleApplication.getApplicationInstance(),
            R.raw.notification_sound);
}

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
        }
    }
});

mp.start();