android - 唤醒设备,一旦进入打盹模式
问题描述
我正在开发应用程序来检查哪些必须连接到 MQTT 服务器才能从服务器获取消息。当 android 设备进入打盹模式时,客户端(Android)会自动断开连接,因为网络已暂停但处于活动状态。
一旦设备进入打盹模式,我需要唤醒我的设备,连接到使用 MQTT 的服务器
我已经为此编写了代码,我需要确保我是否在正确的轨道上?
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isIdle = pm.isDeviceIdleMode();
Log.d(TAG, " --- DozeModeReceiver isIdle--- "+isIdle);
if (isIdle) {
// the device is now in doze mode
if (screenWakeLock == null) {
PowerManager mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
screenWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakeLockTag");
Log.d(TAG, " --- Going to Wake Screen ... --- " + result);
screenWakeLock.acquire();
try {
/**
*Code to connect with MQTT Server
*/
} catch (Exception e) {
e.printStackTrace();
}
}
if (screenWakeLock != null) {
screenWakeLock.release();
Log.i(TAG, "screenWakeLock Released ");
}
} else {
// the device just woke up from doze mode
Log.d(TAG, " --- Woke up from Doze mode --- "+isIdle);
}}
解决方案
推荐阅读
- c# - 如何修复 snmp v3 错误“请求已达到最大重试次数。”?
- django - 未找到带有参数“(”,)”的“详细信息”的反向。尝试了 1 种模式:['polls/(?P
[0-9]+)/$'] - keras - 将输入张量与负 1 张量的倍数连接起来
- reactjs - Material-Table onRowClick 波浪动画;这可能吗?
- docker - 尝试启动我的 docker 实例并被告知容器正在使用中,但随后“docker ps”什么也没显示
- javascript - 在执行命令并移动到 node.js 中的其他语音通道后,如何检查该人所在的语音通道 ID?
- java - 您可以在网页上运行 java 可执行文件吗?
- php - PHP - 需要 __construct() 的帮助
- c - 定义与另一个数组相反的数组时遇到问题
- python - 尝试导入 pandas 时,得到“进程已完成,退出代码 132(被信号 4:SIGILL 中断)”