Android默认音调选择器问题(带通知和闹钟的默认音调)

时间:2014-02-27 18:54:53

标签: android uri picker ringtone

我不知道这是一个容易解决的愚蠢问题。我有一个铃声选择器,显示默认铃声选项(用于通知)。像这样:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
startActivityForResult(intent, RingtoneManager.TYPE_NOTIFICATION);

然后我抓住了结果:

protected void onActivityResult(int requestCode, int resultCode, Intent mRingtone) {
    switch (requestCode) {
        case RingtoneManager.TYPE_NOTIFICATION:
            if (resultCode == RESULT_OK) {
                notifToneUri = mRingtone.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
                Log.d(TAG,"Uri = " + notifToneUri.toString() + " and title = " + ringtone.getTitle(this));
            }
        break;
    } 
}

发生奇怪的事情,如果我选择“默认通知音”选项,则uri应该是:* content:// settings / system / notification_sound *但是uri是内容:// settings / system / ringtone (这是铃声的默认值,而不是通知)。它与默认警报音相同。

我这样解决了:

if (notifToneUri.equals(Settings.System.DEFAULT_RINGTONE_URI)){
    notifToneUri = Settings.System.DEFAULT_NOTIFICATION_URI;
}

好的,这有效...但我想知道是否有更好的方法,或者我做错了。

提前谢谢。

1 个答案:

答案 0 :(得分:10)

我认为您没有正确检索Uri。以下是一个示例:

启动RingtoneManager

final Intent ringtone = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
ringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
ringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
ringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
startActivityForResult(ringtone, 0);

Uri

中检索Activity.onActivityResult和标题
if (requestCode == 0 && resultCode == RESULT_OK) {
    final Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    final Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
    // Get your title here `ringtone.getTitle(this)`
}

您可以看到here in the source for Ringtone,只有当Uri的权威性等于Settings.AUTHORITY且永远不会MediaStore.AUTHORITY时,才会添加前缀。