在Android上更改默认通知铃声

时间:2014-03-21 11:06:08

标签: android notifications ringtone

我试图在我的应用中更改通知铃声。到目前为止,我可以显示通知铃声,然后选择其中一个,但它没有改变它。这是我到目前为止所使用的:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        Uri notification = Uri.parse(settings.getString("timerSound", RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString()));

        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Alert Tone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, notification);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);     
        startActivityForResult(intent, 5);

1 个答案:

答案 0 :(得分:0)

您可以将通知铃声更改为以下代码

//new ringtone uri
Uri nuri = Uri.parse("");

//change notification
RingtoneManager.setActualDefaultRingtoneUri(this,RingtoneManager.TYPE_NOTIFICATION, nuri);

如果您想更改铃声,可以使用以下代码

    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
for (int volumeType : (new int[] { AudioManager.STREAM_SYSTEM,
    AudioManager.STREAM_RING,AudioManager.STREAM_NOTIFICATION,AudioManager.STREAM_ALARM })) {
        int maxVolume = audio.getStreamMaxVolume(volumeType);
        audio.setStreamVolume(volumeType, maxVolume,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE);
        audio.setStreamMute(volumeType, false);
        audio.setVibrateSetting(volumeType, AudioManager.VIBRATE_SETTING_ON);

谢谢