AlarmManager锁屏后设置的定时被取消的解决方案

我用alarmmanager设置了定时任务,但是锁屏的时候根本不会执行。查了大量资料,还是无法解决,有好的办法吗?(测试手机:华为相关代码如下:
if (Build.VERSION.SDK_INT >= 19) {
                            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP,
                                    course.getStartTime().getTimeInMillis(),
                                    vibrationPI);

                            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP,
                                    course.getEndTime().getTimeInMillis(),
                                    normalPI);
                        } else {
                            mAlarmManager.set(AlarmManager.RTC_WAKEUP,
                                    course.getStartTime().getTimeInMillis(),
                                    vibrationPI);

                            mAlarmManager.set(AlarmManager.RTC_WAKEUP,
                                    course.getEndTime().getTimeInMillis(),
                                    normalPI);
                        }


public class SetVibrationService extends IntentService {
    private static final String NAME = "SetVibrationService";
    private static final int NOTIFICATION_ID = 1;

    public SetVibrationService() {
        super(NAME);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        AudioManager audioManager =
                (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 1);

        NotificationUtil.
                sendNotification(this, NOTIFICATION_ID, R.string.vibrate_service_notification_text);
        Log.d("ApplicationLog", "VibrateService" + new Date());
    }
}


public class SetNormalService extends IntentService {
    private static final String NAME = "SetNormalService";
    private static final int NOTIFICATION_ID = 2;

    public SetNormalService() {
        super(NAME);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        AudioManager audioManager =
                (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 2, 1);

        NotificationUtil.
                sendNotification(this, NOTIFICATION_ID, R.string.normal_service_notification_text);

        Log.d("ApplicationLog", "NormalService" + new Date());
    }
}

展开阅读全文

没有更多推荐了,返回首页