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

时间:2010-04-27 20:32:18

标签: java android audio notifications

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

5 个答案:

答案 0 :(得分:100)

只需从我的某个应用程序中复制/粘贴一些代码,这些代码可以满足您的需求。

这是一个标记为“set ringtone”或类似内容的按钮的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,而不仅仅是内置的铃声。

答案 1 :(得分:49)

或者只是将其添加到首选项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>

答案 2 :(得分:16)

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

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,而不是其他方法获得的人类可读标题。结合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;
}

上面的代码将返回一个字符串列表,例如&#34; content:// media / internal / audio / media / 27&#34; ..然后您可以传递其中一个字符串作为一个Uri进入.setSound()就像:

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

希望足够清楚:)

答案 3 :(得分:1)

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

答案 4 :(得分:0)

这是另一种方法(在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
    }

可能需要进行一些重构和优化,但是您应该了解一下。