在android中设置自定义警报音

时间:2012-12-08 19:56:20

标签: android alarm

您好我想知道如何设置自定义闹铃音。

这是我的代码:

RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_ALARM,
                    newUri
                    );

它适用于通知/声音但不适用于警报(:S)?

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以使用此代码

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
 if(alert == null){
     // alert is null, using backup
     alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase
         // alert backup is null, using 2nd backup
         alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
     }
 }

答案 1 :(得分:0)

试试这个:

Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  playRingtone(RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri));

然后,编写另一种方法 playRingtone(Ringtone newRingtone)来播放铃声。

private void playRingtone(Ringtone newRingtone) {

        if(null != mCurrentRingtone && mCurrentRingtone.isPlaying())
            mCurrentRingtone.stop();

            mCurrentRingtone = newRingtone;

        if(null != newRingtone){
            mCurrentRingtone.play();
        }
    }