2015-06-04 59 views
1

我用一个简单的选择对话框让用户选择一个通知声音,这里的代码开始选择器:RingtoneManager显示手机铃声,而不是通知声音

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.selectSound)); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(LocalCfg.getNotificationSound())); 
startActivityForResult(intent, SELECT_RINGTONE_REQUEST); 

LocalCfg.getNotificationSound()简单地检查内部SharedPreferences设置,在情况下返回默认通知声音Uri设置尚不存在:在所有测试的手机观察

public static String getNotificationSound() { 
    return mPrefs.getString(KEY_PREF_NOTIFY_SOUND_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString()); 
} 

问题:“去故障“列出的声音不是通知/报警声音,而是实际的电话铃声(系统默认或由用户设置的自定义)。

某些手机(Samsung Galaxy Young,Xperia Z1 Compact)显示为“默认通知音”(实际上是错误的),其他一些手机(Nexus设备,SDK 22)显示为“默认铃声”。

为什么会发生如果我明确通过RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM标志?

回答

0

我看过不同的做法,因为我的手机有这个问题,而且Whatsapp似乎自己绕过它(在我的手机上播放自己的音调)。

从我的研究,唯一可行的方法是检查长度(see this answer),并发挥自己的语气,如果该文件是不可能的,这里是我的代码:

//Create default notification ringtone 
MediaPlayer mp = MediaPlayer.create(LinxaleApplication.getApplicationInstance(), 
     RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

//NOTE: not checking if the sound is 0 length, becuase then it should just not be played 
if (mp.getDuration() > 5000 /* Ringtone, not notification, happens on HTC 1 m7 5.X version */) { 
    mp.release(); 
    mp = MediaPlayer.create(LinxaleApplication.getApplicationInstance(), 
      R.raw.notification_sound); 
} 

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
    @Override 
    public void onCompletion(MediaPlayer mediaPlayer) { 
     if (mediaPlayer != null) { 
      if (mediaPlayer.isPlaying()) { 
       mediaPlayer.stop(); 
      } 
      mediaPlayer.release(); 
     } 
    } 
}); 

mp.start(); 
1

使用RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI额外:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.selectSound)); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(LocalCfg.getNotificationSound())); 
startActivityForResult(intent, SELECT_RINGTONE_REQUEST);