如何在Android上显示可用通知声音列表

2022-08-31 14:04:57

我正在我的 Android 应用程序中创建通知,并希望在我的首选项中有一个选项来设置通知使用的声音。我知道在“设置”应用程序中,您可以从列表中选择默认通知声音。该列表来自哪里,有没有办法在我的应用程序中显示相同的列表?


答案 1

只需从我的一个应用程序中复制/粘贴一些代码,即可完成您正在寻找的功能。

这是在标记为“设置铃声”或类似内容的按钮的onClick处理程序中:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 5);

此代码捕获了用户所做的选择:

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    if (resultCode == Activity.RESULT_OK && requestCode == 5) {
        Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

        if (uri != null) {
            this.chosenRingtone = uri.toString();
        } else {
            this.chosenRingtone = null;
        }
    }            
}

另外,我建议我的用户从Android市场安装“戒指扩展”应用程序。然后,每当在他们的设备上打开此对话框时,例如从我的应用程序或手机的设置菜单中,用户都可以选择存储在其设备上的任何mp3,而不仅仅是内置铃声。


答案 2

或者只是把它贴在你的首选项XML中:

  <RingtonePreference android:showDefault="true"
     android:key="Audio" android:title="Alarm Noise"
     android:ringtoneType="notification" />

我的示例 XML 的完整内容仅用于上下文:

<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Some value"
                    android:key="someval"
                    android:summary="Please provide some value" />
<EditTextPreference android:title="Some other value"
                    android:key="someval2"
                    android:summary="Please provide some other value" />
 <RingtonePreference android:showDefault="true"
     android:key="Audio" android:title="Alarm Noise"
     android:ringtoneType="notification" />

</PreferenceScreen>