Skip to content

Commit baa9b42

Browse files
committed
增加防止触发异常导致wakelock未被释放问题
1 parent 20a8fac commit baa9b42

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

hub/hub-device-android/src/main/java/com/tencent/iot/hub/device/android/core/mqtt/TXAlarmPingSender.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void onReceive(Context context, Intent intent) {
158158
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
159159
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
160160
try { // 唤醒锁获取失败,抛出权限异常,同时避免后续的释放过程
161-
wakelock.acquire();
161+
wakelock.acquire(60000);
162162
} catch (Exception e) {
163163
TXLog.e(TAG, "wakelock without permission.WAKE_LOCK return");
164164
e.printStackTrace();
@@ -168,26 +168,47 @@ public void onReceive(Context context, Intent intent) {
168168
// Assign new callback to token to execute code after PingResq
169169
// arrives. Get another wakelock even receiver already has one,
170170
// release it until ping response returns.
171-
IMqttToken token = mComms.checkForActivity(new IMqttActionListener() {
171+
IMqttToken token = null;
172+
try {
173+
token = mComms.checkForActivity(new IMqttActionListener() {
174+
175+
@Override
176+
public void onSuccess(IMqttToken asyncActionToken) {
177+
TXLog.d(TAG, "Success. Release lock(" + wakeLockTag + "):" + System.currentTimeMillis());
178+
//Release wakelock when it is done.
179+
safeReleaseWakeLock();
180+
}
181+
182+
@Override
183+
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
184+
TXLog.d(TAG, "Failure. Release lock(" + wakeLockTag + "):" + System.currentTimeMillis());
185+
//Release wakelock when it is done.
186+
safeReleaseWakeLock();
187+
}
188+
});
189+
} catch (Exception e) {
190+
TXLog.e(TAG, "checkForActivity exception: " + e.getMessage());
191+
e.printStackTrace();
192+
}
172193

173-
@Override
174-
public void onSuccess(IMqttToken asyncActionToken) {
175-
TXLog.d(TAG, "Success. Release lock(" + wakeLockTag + "):" + System.currentTimeMillis());
176-
//Release wakelock when it is done.
177-
wakelock.release();
178-
}
194+
if (token == null) {
195+
TXLog.d(TAG, "Token is null. Release lock immediately.");
196+
safeReleaseWakeLock();
197+
}
198+
}
179199

180-
@Override
181-
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
182-
TXLog.d(TAG, "Failure. Release lock(" + wakeLockTag + "):" + System.currentTimeMillis());
183-
//Release wakelock when it is done.
200+
/**
201+
* 安全释放 wake lock
202+
*/
203+
private void safeReleaseWakeLock() {
204+
try {
205+
if (wakelock != null && wakelock.isHeld()) {
184206
wakelock.release();
207+
TXLog.d(TAG, "WakeLock released successfully");
185208
}
186-
});
187-
188-
189-
if (token == null && wakelock.isHeld()) {
190-
wakelock.release();
209+
} catch (Exception e) {
210+
TXLog.e(TAG, "Error releasing wakelock: " + e.getMessage());
211+
e.printStackTrace();
191212
}
192213
}
193214
}

0 commit comments

Comments
 (0)