广播接收器不触发的 AlarmManager

AlarmManager With BroadcastReceiver Not Firing

我有一个 AlarmManager 设置在 BroadcastReceiver 子类中,使用 Service 子类作为上下文。 AlarmManager 没有开火。我知道 AlarmManager 设置了正确的毫秒数。

对于为什么 AlarmManager 没有触发有任何想法吗?

public class Alarm extends BroadcastReceiver
    {
        public static final String TAG = "Alarm";
        public MediaPlayer mediaPlayer;
        public AlarmManager alarmManager;

        @Override
        public void onReceive(Context context, Intent intent)
        {
            Log.d(TAG, "Alarm set off onReceive");

            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
            wl.acquire();

            //Put to code to execute upon alarm here

            //Check to see if the sounds are playing, if they are stop them.
            if (mediaPlayer.isPlaying()){
                //Call play will simulate the user tapping the play button which will stop the sounds.
                mediaPlayer.stop();
                mediaPlayer.release();
            }

            wl.release();
        }

        public void setAlarm(Context context, long milliSeconds, MediaPlayer mp)
        {
            //Set the mediaPlayer member var
            mediaPlayer = mp;

            //Figure out the time to stop the timer in milliseconds
            long currentTime = System.currentTimeMillis();
            long timeToSetAlarmTo = currentTime+milliSeconds;
            Log.d(TAG, "Current Time = " + currentTime + "Time to add = " + milliSeconds + "Time to set timer to = " + timeToSetAlarmTo);

            //Create the intent
            Intent i = new Intent("SmartlifeSoftware.START_ALARM");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, i, 0);

            //Set the alarm
            alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToSetAlarmTo, pendingIntent);

        }

        public void CancelAlarm(Context context)
        {
            Intent intent = new Intent("SmartlifeSoftware.START_ALARM");
            PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
            alarmManager.cancel(sender);
        }
    }

这是我在应用程序标签下的清单代码

    <receiver android:name="services.Alarm" android:exported="true">
        <intent-filter>
            <action android:name="SmartlifeSoftware.START_ALARM">
            </action>
        </intent-filter>
    </receiver>`

我解决了这个问题。在 alarmManager.set 方法中,我将 int type 参数从 AlarmManager.ELAPSED_REALTIME_WAKEUP 更改为 AlarmManager.RTC_WAKEUPRTC_WAKEUP 指定触发时间。

来自文档:

RTC_WAKEUP—唤醒设备以在指定时间触发未决意图。

ELAPSED_REALTIME_WAKEUP-SystemClock.elapsedRealtime()中的闹钟时间(开机后的时间,包括休眠),关机时会唤醒设备。

您的警报不会因 AlarmManager.ELAPSED_REALTIME_WAKEUP 而触发,因为您的 triggerAtMilis 参数错误。你应该改变:

long currentTime = System.currentTimeMillis();
long timeToSetAlarmTo = currentTime+milliSeconds;

long currentTime = SystemClock.elapsedRealtime()
long timeToSetAlarmTo = currentTime+milliSeconds;