为什么闹钟叫了却不响?

Why is alarm being called but not ringing?

我是报警管理员,

     Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

这是我的接收器:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtone = RingtoneManager.getRingtone(arg0, uri);
    ringtone.play();
    if (ringtone.isPlaying()) {
        Log.e("Yep","Its playin");
    }else {
        Log.e("NOp","just nop");
    }

当呼叫接收器时,我总是得到是的,它正在播放但铃声从未响起...它不是我确定的音量,所以还有什么问题?提前谢谢你

尝试改用 MediaPlayer:

Uri uri = RingtoneManager.getDefaultUri(RingtoneManager. TYPE_ALARM);
try{
    MediaPlayer mp = MediaPlayer.create(arg0, uri);
    mp.start();
} catch(Exception e){}

也可以尝试使用 RingtoneManager.TYPE_NOTIFICATION 而不是 RingtoneManager.TYPE_ALARM

您正在使用模拟器吗?因为它默认没有铃声。 如果你是,你可以按原样使用你的代码(因为它应该在实际设备上工作,但如果没有铃声等可能会失败,例如静音模式。)通过执行以下操作:

使用 DDMS 在您的 /sdcard 文件夹中上传一个 MP3 文件,重新启动模拟器,然后打开媒体应用程序,浏览到您的 MP3 文件,长按它并 select "Use as phone ringtone"。