2010-04-27 113 views
56

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

回答

92

只需从我的一个应用程序中复制/粘贴一些代码,即可执行您正在查找的内容。

这是一个按钮,标有“设置铃声”或类似的东西一个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 Market安装“Rings Extended”应用程序。然后,无论何时在设备上打开此对话框(例如从我的应用程序或手机的设置菜单中打开),用户都可以选择存储在设备上的任何MP3,而不仅仅是内置铃声。

+0

谢谢,我是能够成功地使用你的代码显示铃声但在** onActivityResult()方法**我得到错误“不能从静态引用到非静态方法getParcelableExtra(String)键入意图“在你的线”Uri uri = Intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);“ – Dharmendra 2011-10-20 08:47:33

+1

该行应为“Uri uri = intent.getParcelableExtra ...”。注意“intent”中的小写字母“i”。上面的代码只是一个错字。 – zeh 2011-12-12 21:13:57

+0

有用的代码,并介意intent.getParcelableExtra中的小写字母... 非常感谢 – djk 2012-02-04 06:19:33

46

或者只是坚持这自己的喜好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> 
+0

这一个看起来更好.. – Mohsen 2017-02-16 12:29:51

13

这是我用得到通知声音的列表的方法可用在手机:)

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

上面的代码将返回一个字符串列表,像“的内容:// /内部/音频/媒体/ 27” ..你可以再通过这些字符串中的一个作为乌里进入。 setSound(),如:

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

希望那是足够清晰的:)

+0

这很好,但你如何设置与NotificationCompat.Builder.setSound(uri)播放声音?设置其中一个来自RingtoneManager的Uri阅读不起作用 – infero 2015-05-04 11:32:15

+0

我已经使用答案@infero编辑了帖子,希望它很明确并解决您的问题! – Murphybro2 2015-05-05 08:19:18

+0

感谢您的更新。我试图用id这种方式设置声音,但使用了一个不存在的id::(grrrr。再次感谢 – infero 2015-05-05 09:56:23