安卓AlarmManager没有醒来电话了

编程入门 行业动态 更新时间:2024-10-27 21:17:02
本文介绍了安卓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没有醒来电话了

本文发布于:2023-11-27 14:44:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电话   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!