Android:使用Alarm唤醒设备

时间:2013-04-10 10:59:24

标签: 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);

当闹钟响起时,我执行了显示新活动的相同代码并再现了声音。这很好,但如果我的设备处于睡眠模式,当闹钟响起时,我只能听到声音。没有显示任何活动,我的设备仍处于休眠状态。

当闹钟响起时,我该怎么办才能自动唤醒设备?

编辑:

我尝试了以下内容:

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

此外,我对获取方法有这个警告:

  

找到了一个唤醒锁获取()但没有发布任何版本的调用()

2 个答案:

答案 0 :(得分:8)

Receiver活动中尝试以下代码:

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

答案 1 :(得分:2)

只需在Android 4.4版之后编辑,{@ 1}}方法已弃用,并且已引入setInexactRepeating()

请通过此更新博客设置闹钟: https://developer.android.com/training/scheduling/alarms.html