安卓AlarmManager没有醒来电话了 [英] android AlarmManager not waking phone up

查看:172
本文介绍了安卓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.

我的code设定报警:

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

这code不会唤醒器件。然而,当我把它回来了,它们显示。我需要让他们打开屏幕。你能帮我解决这个问题?

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。应该做(preferably作为接收器的第一件事),或在接收到报警时,该设备将被唤醒,但会前再次入睡context.startActivity(newIntent);被调用。 (我还观察到的行为时,没有发生,因此,这似乎是有点任意) 因此,方便,快捷的答案: 建立一个新类叫WakeLocker与此源$ C ​​$ C:

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(上下文); 作为第一个事情。 附加说明:这也将是整齐叫 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.

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

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