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

移动开发 码拜 8年前 (2016-09-25) 1992次浏览
本人用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());
    }
}
解决方案

10

本人用过华为pad 他的闹钟最低间隔时间好像是5秒  用的是audioManager.setRepeating    同样的代码  在小米手机上可行(锁屏睡眠后正常运行,本人设置的是1秒一次)

20

你这个不会执行本人估计是还没执行到定时那去就睡眠了   睡眠后线程,服务都挂起了  估计是一直没有执行到开启闹钟那里。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明AlarmManager锁屏后设置的定时被取消的解决方案
喜欢 (0)
[1034331897@qq.com]
分享 (0)