其他分享
首页 > 其他分享> > Android:使用Alarm唤醒设备

Android:使用Alarm唤醒设备

作者:互联网

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

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

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

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,alarmmanager,wakelock
来源: https://codeday.me/bug/20190718/1493362.html