android AlarmManager 没有唤醒电话 [英] android AlarmManager not waking phone up

查看:30
本文介绍了android AlarmManager 没有唤醒电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在特定时间显示活动.为此,我正在使用 AlarmManager.当设备处于唤醒状态时它工作正常,但如果它处于睡眠状态则不会唤醒它.

I want an activity to be displayed at a certain time. For this, I am using AlarmManager. It works fine when the device is awake, but it doesn't wake it up if it's asleep.

我设置闹钟的代码:

Calendar alarmTime = Calendar.getInstance();
alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour);
alarmTime.set(Calendar.MINUTE, alarm.minute);
alarmTime.set(Calendar.SECOND, 0);

if (alarmTime.before(now))
    alarmTime.add(Calendar.DAY_OF_MONTH, 1);

Intent intent = new Intent(ctxt, AlarmReceiver.class);
intent.putExtra("alarm", alarm);
PendingIntent sender = PendingIntent.getBroadcast(ctxt, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), sender);

我的广播接收器:

@Override
public void onReceive(Context context, Intent intent) {
    try {

Bundle bundle = intent.getExtras();
final Alarm alarm = (Alarm) bundle.getSerializable("alarm");

Intent newIntent;
if (alarm.type.equals("regular")) {
    newIntent = new Intent(context, RegularAlarmActivity.class);
} else if (alarm.type.equals("password")) {
    newIntent = new Intent(context, PasswordAlarmActivity.class);
} else if (alarm.type.equals("movement")) {
    newIntent = new Intent(context, MovementAlarmActivity.class);
} else {
    throw new Exception("Unknown alarm type");
}
    newIntent.putExtra("alarm", alarm);
    newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(newIntent);

} catch (Exception e) {
    Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
    Log.e("AlarmReceiver", Log.getStackTraceString(e));
}
}

此代码不会唤醒设备.但是,当我再次将其转回去时,它们会显示出来.我需要让他们打开屏幕.你能帮我解决这个问题吗?

This code doesn't wake the device on. However, when I turn it back again, they are displayed. I need to make them turn the screen on. Can you help me with this problem?

推荐答案

我遇到了类似的问题,解决方案是使用 WakeLocker.应该这样做(最好是接收器中的第一件事),否则设备会在收到警报时唤醒,但会在 context.startActivity(newIntent) 之前再次入睡;叫做.(我也观察到没有发生这种情况时的行为,所以这似乎有点武断)所以简单快捷的答案:使用此源代码创建一个名为 WakeLocker 的新类:

I had a similar problem and the solution was to use WakeLocker. That should be done (preferably as the 1st thing in the receiver), or the device will wake up when the alarm is received, but will fall asleep again before context.startActivity(newIntent); is called. (I have also observed behavior when that does not happen, so it seems to be a bit arbitrary) So the easy and quick answer: Make a new class called WakeLocker with this source code:

package mypackage.test;

import android.content.Context;
import android.os.PowerManager;

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context ctx) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

并在您的接收器中调用 WakeLocker.acquire(context); 作为第一件事.额外的:一旦你的闹钟完成了它的事情,调用 WakeLocker.release(); 也很整洁.

and in your receiver call WakeLocker.acquire(context); as the 1st thing. Extra: it would also be neat to call WakeLocker.release(); once your alarm has done its thing.

这篇关于android AlarmManager 没有唤醒电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆