2

先生,即使铃声被静音,是否可以响铃?如果是,如何。而且,我可以将它的音量设置在两者之间,而不仅仅是 maxvolume 吗?提前致谢

for (SmsMessage msg : messages) {
        if (msg.getMessageBody().contains("firealert")) {

            Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
             if(alert == null){
                 // alert is null, using backup
                 alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                 if(alert == null){  
                     // alert backup is null, using 2nd backup
                     alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
                 }
             }
             Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
             AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
             int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
             int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
             audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
             audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
             r.play();
                Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();


        }//end if
    }//end for
4

1 回答 1

5

要看的东西:

AudioManager.getRingerMode / setRingerMode。将其设置为 RINGER_MODE_NORMAL 以确保手机没有静音或设置为振动。(尽管您可能想先检查状态,以便您可以存储它并在清除警报后重置值。)

if ( aManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL )
    aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

例如,如果要将音量设置为 50%,请检查 AudioManager.getStreamMaxVolume,然后将其除以 2。不过,让用户配置它可能会更好。使用 SeekBar 让他设置首选音量(同样,您可以在闹钟响起后恢复正常)。

RingerVolumeBar = (SeekBar) findViewById(R.id.seekBar1);
RingerVolumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
   @Override
   public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser)
   {
   aManager.setStreamVolume(AudioManager.STREAM_RING,
                            progress, AudioManager.FLAG_PLAY_SOUND);
   // record new setting so you can load it up or reset it back later
   ...
   }
});
于 2012-12-04T14:34:02.363 回答