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

rob*_*ntw 63 java audio notifications android

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

Mar*_*k B 101

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

这是一个标记为"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);
Run Code Online (Sandbox Code Playgroud)

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

 @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;
          }
      }            
  }
Run Code Online (Sandbox Code Playgroud)

另外,我建议我的用户从Android电子市场安装"Rings Extended"应用程序.然后,只要在他们的设备上打开此对话框,例如从我的应用程序或手机的设置菜单中打开,用户就可以选择选择存储在其设备上的任何mp3,而不仅仅是内置的铃声.

  • 要获取铃声的名称,请执行以下操作:铃声铃声= RingtoneManager.getRingtone(context,uri); String title = ringtone.getTitle(this); (4认同)
  • 我应该如何获取从ringtonePicker意图返回的铃声的名称? (3认同)

JD.*_*JD. 49

或者只是将其粘贴在您的首选项XML中:

  <RingtonePreference android:showDefault="true"
     android:key="Audio" android:title="Alarm Noise"
     android:ringtoneType="notification" />
Run Code Online (Sandbox Code Playgroud)

我的示例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>
Run Code Online (Sandbox Code Playgroud)


Mur*_*ro2 18

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

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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是关于如何在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;
}
Run Code Online (Sandbox Code Playgroud)

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

.setSound(Uri.parse("content://media/internal/audio/media/27"))
Run Code Online (Sandbox Code Playgroud)

希望很清楚:)