4

我正在使用以下代码设置警报:

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 并重现声音。这很好,但如果我的设备处于睡眠模式,当闹钟响起时,我只能听到声音。没有显示任何活动,我的设备保持外观并处于睡眠模式。

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

编辑:

我尝试了以下方法:

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

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

找到了一个唤醒锁获取(),但在任何地方都没有释放()调用

4

2 回答 2

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);
于 2013-04-10T11:13:17.970 回答
2

只是为了编辑,在 Android 4.4 版之后,该setRepeating方法已被弃用,他们已经引入setInexactRepeating().

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

于 2017-11-03T14:23:42.257 回答