android AlarmManager 无法唤醒手机

发布于 2024-11-26 15:25:17 字数 1661 浏览 1 评论 0原文

我想要在某个时间显示一个活动。为此,我使用 AlarmManager。 当设备唤醒时它工作正常,但如果设备处于睡眠状态则不会唤醒它。

我的设置闹钟的代码:

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));
}
}

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

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.

My code for setting the alarm:

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);

My broadcast receiver:

@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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七度光 2024-12-03 15:25:17

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

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(); 也会很方便。

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;
    }
}

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.

北方的巷 2024-12-03 15:25:17

最有可能的是,警报正在唤醒设备。但是,AlarmManager 广播不会打开屏幕,并且设备很可能在您的 Activity 启动之前重新进入睡眠状态。

您需要在调用 startActivity() 之前在 onReceive() 中获取 WakeLock,并在调用之后释放该 WakeLock用户响应您的活动。

Most likely, the alarm is waking up the device. However, AlarmManager broadcasts won't turn the screen on, and the device may well fall back asleep before your activity starts up.

You will need to acquire a WakeLock in onReceive() before calling startActivity(), and release that WakeLock after the user responds to your activity.

§对你不离不弃 2024-12-03 15:25:17

对于服务(也许也适用于活动),从 WakefulBroadcastReceiver 扩展您的 AlarmReceiver,它会在处理 Intent 时为您获取 WAKE_LOCK。

WakefulBroadcastReceiver 文档 -
https://developer.android.com/reference/android/support /v4/content/WakefulBroadcastReceiver.html

保持设备唤醒指南 -
https://developer.android.com/training/scheduling/wakelock.html

For Services (maybe works for activity as well), extend your AlarmReceiver from WakefulBroadcastReceiver, it acquires WAKE_LOCK for you while intent is being processed.

WakefulBroadcastReceiver docs -
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

Keeping device awake guide -
https://developer.android.com/training/scheduling/wakelock.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文