我已经在Google Play上上传了我的应用程序,但是用户报告了以下异常:java.lang.RuntimeException: WakeLock under-locked C2DM_LIB。当我尝试释放
WakeLock
时会出现此异常。有人可以告诉我可能的问题是什么吗?WakeLock
时会出现此异常。有人可以告诉我可能的问题是什么吗?if (mWakeLock.isHeld())
mWakeLock.release();
然后异常就消失了。
sWakeLock.release();
并将其用try-catch包围。
代码应该像这样:
synchronized (LOCK) {
// sanity check for null as this is a public method
if (sWakeLock != null) {
Log.v(TAG, "Releasing wakelock");
try {
sWakeLock.release();
} catch (Throwable th) {
// ignoring this exception, probably wakeLock was already released
}
} else {
// should never happen during normal workflow
Log.e(TAG, "Wakelock reference is null");
}
}
更新:
同时,如@fasti在他的回答中建议的那样,您可以使用mWakeLock.isHeld()
方法来检查wakelock是否实际持有此锁。
if (mWakeLock == null) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
}
try{
mWakeLock.release();//always release before acquiring for safety just in case
}
catch(Exception e){
//probably already released
Log.e(TAG, e.getMessage());
}
mWakeLock.acquire();