如何在 Android 上调出可用通知声音列表

发布于 2024-08-30 07:58:10 字数 119 浏览 2 评论 0原文

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

I'm creating notifications in my Android application, and would like to have an option in my preferences to set what sound is used for the notification. I know that in the Settings application you can choose a default notification sound from a list. Where does that list come from, and is there a way for me to display the same list in my application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事与诗 2024-09-06 07:58:10

只需从我的一个应用程序中复制/粘贴一些代码即可完成您所需的操作。

这是在标有“设置铃声”或类似内容的按钮的 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 市场安装“Rings Extended”应用程序。然后,每当在他们的设备上打开此对话框时,例如从我的应用程序或手机的设置菜单中,用户将有额外的选择来选择存储在其设备上的任何 mp3,而不仅仅是内置铃声。

Just copy/pasting some code from one of my apps that does what you are looking for.

This is in an onClick handler of a button labeled "set ringtone" or something similar:

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);

And this code captures the choice made by the user:

@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;
        }
    }            
}

Also, I advise my users to install the "Rings Extended" app from the Android Market. Then whenever this dialog is opened on their device, such as from my app or from the phone's settings menu, the user will have the additional choice of picking any of the mp3s stored on their device, not just the built in ringtones.

沫雨熙 2024-09-06 07:58:10

或者只是将其粘贴在您的首选项 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>

Or just stick this in your preferences XML:

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

Full content of my sample XML just for context:

<?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>
动听の歌 2024-09-06 07:58:10

这是我用来获取手机中可用的通知声音列表的方法:)

public Map<String, String> getNotifications() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_NOTIFICATION);
    Cursor cursor = manager.getCursor();

    Map<String, String> list = new HashMap<>();
    while (cursor.moveToNext()) {
        String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
        String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

        list.put(notificationTitle, notificationUri);
    }

    return list;
}

编辑:这是关于如何在NotificationCompat.Builder 中设置声音的评论。此方法获取手机使用的铃声 ID,而不是其他方法获取的人类可读的 TITLE。结合 uri 和 id,您就得到了铃声位置。

public ArrayList<String> getNotificationSounds() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_NOTIFICATION);
    Cursor cursor = manager.getCursor();

    ArrayList<String> list = new ArrayList<>();
    while (cursor.moveToNext()) {
        String id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
        String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

        list.add(uri + "/" + id);
    }

    return list;
}

上面的代码将返回一个字符串列表,例如 "content://media/internal/audio/media/27".. 然后您可以将这些字符串之一作为 Uri 传递到 .setSound( )喜欢:

.setSound(Uri.parse("content://media/internal/audio/media/27"))

希望这足够清楚了:)

This is the method I use to get a list of notification sounds available in the phone :)

public Map<String, String> getNotifications() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_NOTIFICATION);
    Cursor cursor = manager.getCursor();

    Map<String, String> list = new HashMap<>();
    while (cursor.moveToNext()) {
        String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
        String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

        list.put(notificationTitle, notificationUri);
    }

    return list;
}

EDIT: This is for the comment regarding how to set the sound in the NotificationCompat.Builder. This method instead gets the ringtone's ID which is what the phone uses, instead of the human readable TITLE the other method got. Combine the uri and the id, and you have the ringtones location.

public ArrayList<String> getNotificationSounds() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_NOTIFICATION);
    Cursor cursor = manager.getCursor();

    ArrayList<String> list = new ArrayList<>();
    while (cursor.moveToNext()) {
        String id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
        String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

        list.add(uri + "/" + id);
    }

    return list;
}

The above code will return a list of strings like "content://media/internal/audio/media/27".. you can then pass one of these strings as a Uri into the .setSound() like:

.setSound(Uri.parse("content://media/internal/audio/media/27"))

Hope that was clear enough :)

在巴黎塔顶看东京樱花 2024-09-06 07:58:10
  public void listRingtones() {
            RingtoneManager manager = new RingtoneManager(this);
            manager.setType(RingtoneManager.TYPE_NOTIFICATION);
           // manager.setType(RingtoneManager.TYPE_RINGTONE);//For Get System Ringtone
            Cursor cursor = manager.getCursor();

            while (cursor.moveToNext()) {
                String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
                String uri = manager.getRingtoneUri(cursor.getPosition());

                String ringtoneName= cursor.getString(cursor.getColumnIndex("title"));



                Log.e("All Data", "getNotifications: "+ title+"-=---"+uri+"------"+ringtoneName);
                // Do something with the title and the URI of ringtone
            }
        }
  public void listRingtones() {
            RingtoneManager manager = new RingtoneManager(this);
            manager.setType(RingtoneManager.TYPE_NOTIFICATION);
           // manager.setType(RingtoneManager.TYPE_RINGTONE);//For Get System Ringtone
            Cursor cursor = manager.getCursor();

            while (cursor.moveToNext()) {
                String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
                String uri = manager.getRingtoneUri(cursor.getPosition());

                String ringtoneName= cursor.getString(cursor.getColumnIndex("title"));



                Log.e("All Data", "getNotifications: "+ title+"-=---"+uri+"------"+ringtoneName);
                // Do something with the title and the URI of ringtone
            }
        }
一瞬间的火花 2024-09-06 07:58:10

这是另一种方法(在 Kotlin 中),根据此问题中的其他答案构建,允许您指定音调的名称,然后播放它:

fun playErrorTone(activity: Activity, context: Context, notificationName: String = "Betelgeuse") {

    val notifications = getNotificationSounds(activity)

    try {
        val tone = notifications.getValue(notificationName)
        val errorTone = RingtoneManager.getRingtone(context, Uri.parse(tone))
        errorTone.play()
    } catch (e: NoSuchElementException) {
        try {
            // If sound not found, default to first one in list
            val errorTone = RingtoneManager.getRingtone(context, Uri.parse(notifications.values.first()))
            errorTone.play()
        } catch (e: NoSuchElementException) {
            Timber.d("NO NOTIFICATION SOUNDS FOUND")
        }
    }
}

private fun getNotificationSounds(activity: Activity): HashMap<String, String> {
    val manager = RingtoneManager(activity)
    manager.setType(RingtoneManager.TYPE_NOTIFICATION)
    val cursor = manager.cursor

    val list = HashMap<String, String>()
    while (cursor.moveToNext()) {
        val id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX)
        val uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX)
        val title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX)

        list.set(title, "$uri/$id")
    }

    return list
}

它可能需要一些重构和优化,但您应该明白这个想法。

Here's another approach (in Kotlin), build from other answers in this question, that allows you to specify the name of the tone, and then play it:

fun playErrorTone(activity: Activity, context: Context, notificationName: String = "Betelgeuse") {

    val notifications = getNotificationSounds(activity)

    try {
        val tone = notifications.getValue(notificationName)
        val errorTone = RingtoneManager.getRingtone(context, Uri.parse(tone))
        errorTone.play()
    } catch (e: NoSuchElementException) {
        try {
            // If sound not found, default to first one in list
            val errorTone = RingtoneManager.getRingtone(context, Uri.parse(notifications.values.first()))
            errorTone.play()
        } catch (e: NoSuchElementException) {
            Timber.d("NO NOTIFICATION SOUNDS FOUND")
        }
    }
}

private fun getNotificationSounds(activity: Activity): HashMap<String, String> {
    val manager = RingtoneManager(activity)
    manager.setType(RingtoneManager.TYPE_NOTIFICATION)
    val cursor = manager.cursor

    val list = HashMap<String, String>()
    while (cursor.moveToNext()) {
        val id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX)
        val uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX)
        val title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX)

        list.set(title, "$uri/$id")
    }

    return list
}

It can probably take some refactoring and optimization, but you should get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文