Android:使用闹钟唤醒设备

标签 android alarmmanager wakelock

我正在使用以下代码设置闹钟:

Intent intent = new Intent(context, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(reminderContext, 0, intent,
    PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()+delay,pendingIntent);

当闹钟响起时,我执行了相同的代码来显示一个新的 Activity 并重现了一个声音。这很好,但如果我的设备处于 sleep 模式,当闹钟响起时我只能听到声音。未显示任何 Activity ,我的设备保持查看状态并处于 sleep 模式。

我该怎么做才能在闹钟响起时自动唤醒我的设备?

编辑:

我尝试了以下方法:

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();

KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();

除此之外这似乎有效:

04-10 13:49:59.260: A/PowerManager(4292): WakeLock finalized while still held: TAG

此外,我在 acquire 方法上有这个警告:

Found a wakelock acquire() but no release() calls anywhere

最佳答案

在您的 Receiver Activity 中尝试以下代码:

Window wind;
wind = this.getWindow();
wind.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

关于Android:使用闹钟唤醒设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15923948/

相关文章:

java - AlarmManager 没有在正确的时间关闭

安卓后台服务和报警

android - 手机锁定时保持应用程序运行的问题

android - 为什么后退按钮关闭 Activity 而不是反转 fragment 事务?

android - Chrome 推送通知如何检索推送数据?

android - 警报管理器无法通知

android - 用户不活动时屏幕变暗

android - PowerManager.PARTIAL_WAKE_LOCK android

android - 如何在android的WebView中设置文本大小

java.lang.ClassCastException : android. widget.TextView