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

我在我的Android应用程序中创build通知,并希望在我的首选项中有一个选项来设置通知使用的声音。 我知道在“设置”应用程序中,您可以从列表中select一个默认通知声音。 这份清单是从哪里来的,有没有办法让我在申请表中显示相同的清单?

只需从我的应用程序中复制/粘贴一些代码,即可查找所需的代码。

这是在一个标签为“set ringtone”或类似的button的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); 

这个代码捕获用户所做的select:

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

另外,我build议我的用户从Android Market安装“Rings Extended”应用程序。 然后,无论何时在设备上打开此对话框,例如从我的应用程序或从手机的设置菜单中打开,用户将可以select存储在设备上的任何MP3,而不仅仅是内置的铃声。

或者只是将其粘贴到您的偏好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> 

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

 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”这样的string列表。然后你可以将这些string中的一个作为Uri传递给.setSound()

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

希望已经清楚了:)