1

在我的主要活动中,我正在执行此操作:

        Calendar calendar = Calendar.getInstance();

        // 9:45 PM 
        calendar.set(Calendar.HOUR_OF_DAY, 21);
        calendar.set(Calendar.MINUTE, 45);
        calendar.set(Calendar.SECOND, 0);
        AlarmManager am = (AlarmManager) this.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getService(this.getApplicationContext(), 0, new Intent(this.getApplicationContext(), MorningReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

        Toast msg = Toast.makeText(NotificationScanner.this,
                "Scheduled: " + calendar.getTime(), Toast.LENGTH_LONG);
        msg.show();

这是我的接收器

public class MorningReceiver extends BroadcastReceiver { 
@Override
public void onReceive(Context context, Intent intent) {
    MediaPlayer mMediaPlayer = new MediaPlayer();
    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    try {
        mMediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.start();
            PackageManager packageManager = context.getPackageManager();
            Intent skype = packageManager.getLaunchIntentForPackage("com.skype.raider");
            context.startActivity(skype);
        }
    }catch(Exception e){
        Toast msg = Toast.makeText(context,
                "Exception thrown", Toast.LENGTH_LONG);
        msg.show();
    }
}

}

主活动中的 Toast 显示,所以我知道警报正在设置,但接收器从未被调用(铃声从未播放,也没有 Toast 异常消息)。有谁知道我的 AlarmManager 在这里做错了什么?我应该在我的清单中做些什么来让接收器工作吗?

谢谢

4

0 回答 0