2015-06-04 60 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);