gpt4 book ai didi

android - 当应用程序提示用户时唤醒设备

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:17 25 4
gpt4 key购买 nike

我正在使用处理程序重复提示用户输入,例如5分钟。当设备进入休眠模式并且屏幕被锁定时,如何在我的应用提示用户输入时唤醒设备?我试过这个但它似乎不起作用。我在 list 中添加了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 广播自定义 Intent 。

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

最佳答案

通常 wakelock 不会实际打开屏幕。所以你应该用

获得唤醒锁

ACQUIRE_CAUSES_WAKEUP

作为附加标志。

关于android - 当应用程序提示用户时唤醒设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6445844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com