我正在使用处理程序来重复提示用户输入每个输入。 5分钟。当设备进入休眠模式并且屏幕锁定时,如何在我的应用程序提示用户输入时唤醒设备?我试过这个,但它似乎没有工作。我已在清单中添加WAKE_LOCK
权限。当应用程序提示用户时唤醒设备用户
class BtHandler extends Handler {
private PowerManager pm;
private WakeLock wl;
@Override
public void handleMessage(Message msg) {
pm = (PowerManager)FixedNode.this.getSystemService(Context.POWER_SERVICE);
if (!pm.isScreenOn()) {
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "TAG");
wl.acquire();
}
FixedNode.this.setAlwaysDiscoverable();
wl.release();
}
}
有没有想法?
编辑:使用AlarmManager
广播自定义意图。
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(300);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
WakeLock wl = null;
if (!pm.isScreenOn()) {
KeyguardLock kl = km.newKeyguardLock("TAG");
kl.disableKeyguard();
wl = pm.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
}
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
wl.release();
}
};
mFilter = new IntentFilter(ACTION_NAME);
Intent mIntent = new Intent(ACTION_NAME);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (120 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
我不认为有可能通过应用程序绕过屏幕锁定。 – Haphazard
在这种情况下你有什么建议?有没有办法让用户知道他们需要提供输入? – springrolls
你将不得不无限期地保持屏幕。这当然会咀嚼电池。 – Haphazard