[英]WakeLock with PARTIAL_WAKE_LOCK is not working
我有一個警報應用程序,用戶聯系了我,報告說他的警報有時會延遲。 我嘗試了不同的方法和理論,最后建議用戶使用默認的Android警報作為備份。
通常鬧鍾會延遲大約一個小時,但上次我的鬧鍾是在 Android 的一個明確建議之后啟動的,該設備處於睡眠狀態並被 Android 的鬧鍾喚醒,讓我的設備也可以“繼續啟動”。
編輯:由於我修改了一些類並且已經收到來自新版本的日志,我正在更改下面的類和日志信息。
這是我正在使用的 CountedWakeLock 類 - 我創建了自己的 HandlerThread 來處理喚醒鎖的延遲釋放,以檢查這是否會影響啟動問題。 它沒有解決問題,但它在日志中產生了有趣的信息(如下)。
public class CountedWakeLock {
private static long TIMEOUT_DEFAULT = DateUtils.MINUTE_IN_MILLIS * 3;
private static WakeLock sWakeLock = null;
private static int sLockCount;
private static class TimeoutReleaseThread extends HandlerThread {
static TimeoutReleaseThread mInstance = null;
Handler mHandler;
private Runnable mReleaseRunnable;
public static TimeoutReleaseThread instance() {
if (mInstance == null) mInstance = new TimeoutReleaseThread();
return mInstance;
}
public TimeoutReleaseThread() {
super("TimeoutReleaseThread HandlerThread");
start();
mHandler = createHandler();
mReleaseRunnable = new Runnable() {
@Override
public void run() {
Utils.log("TimeoutReleaseThread release lock");
releaseLock();
}
};
}
private synchronized Handler createHandler() {
return new Handler(getLooper());
}
public synchronized void postRelease(long timeout) {
mHandler.removeCallbacks(mReleaseRunnable);
mHandler.postDelayed(mReleaseRunnable, timeout);
}
}
public synchronized static void acquireLock(Context context) {
acquireLock(context, TIMEOUT_DEFAULT);
}
public synchronized static void acquireLock(Context context, long timeout) {
if (sWakeLock == null) {
Utils.log("WakeLock creating");
PowerManager pm = (PowerManager) context.getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"AlarmReceiver lock");
sWakeLock.setReferenceCounted(false);
sLockCount = 0;
} else if (sWakeLock.isHeld())
Utils.log("WakeLock held already");
else
Utils.log("WakeLock not held");
Utils.log("WakeLock acquiring for " + timeout);
sLockCount++;
sWakeLock.acquire();
TimeoutReleaseThread.instance().postRelease(timeout);
}
public synchronized static void releaseLock() {
Utils.log("WakeLock releasing");
if (sWakeLock == null) {
Utils.log("WakeLock==null");
} else if (!sWakeLock.isHeld()) {
Utils.log("WakeLock not held");
sWakeLock = null;
sLockCount = 0;
} else {
sLockCount--;
if (sLockCount <= 0) {
Utils.log("WakeLock released");
sWakeLock.release();
sWakeLock = null;
if (sLockCount != 0) Utils.log("lockcount=" + sLockCount);
}
}
}
}
AlarmReceiver BroadcastReceiver - 它正在從 AlarmManager 接收意圖
public void onReceive(Context context, Intent intent) {
Logger.initialize(context, "AlarmReceiver");
if (CALL_IS_ON) {
//set another alarm with AlarmManager to start after 5 seconds
//doesn't happen in this situation
} else {
Utils.log("sending START ALARM");
CountedWakeLock.acquireLock(context);
Intent i = new Intent();
i.setAction(StartAlarmReceiver.ACTION_START_ALARM);
i.putExtras(intent.getExtras());
context.sendOrderedBroadcast(i, null); //it is ordered, so eventual previous Alarm Activities etc. can be stopped
Utils.log("START ALARM send");
}
}
啟動警報接收器。 它實際上是在啟動警報活動
public void onReceive(Context context, Intent intent) {
Logger.initialize(context, "StartAlarmReceiver");
Intent i = new Intent(context, AlarmOnScreen.class);
Bundle extras = intent.getExtras();
i.putExtras(extras);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Utils.log("AlarmActivity started");
}
更新日志:
504. 27/3 5:0:0 - logger initialized again from AlarmReceiver
505. 27/3 5:0:0 - sending START ALARM
506. 27/3 5:0:0 - WakeLock creating
507. 27/3 5:0:0 - WakeLock acquiring for 180000
508. 27/3 5:0:0 - START ALARM send
509. 27/3 5:0:0 - logger initialized again from StartAlarmReceiver
510. 27/3 5:0:0 - AlarmActivity started
511. 27/3 5:0:0 - Main start
512. 27/3 5:0:1 - Main resume
513. 27/3 5:0:1 - Main pause
514. 27/3 5:0:5 - Main stop
515. 27/3 5:3:0 - TimeoutReleaseThread release lock
516. 27/3 5:3:0 - WakeLock releasing
517. 27/3 5:3:0 - WakeLock released
518. 27/3 6:46:18 - logger initialized again from AlarmOnScreen create //user said he unlocked phone then to check the time...
從現在的日志中,我相信 WakeLock 確實在工作 - 畢竟 CPU 必須一直在運行,如果 TimeoutReleaseThread 能夠完成工作。
問題是為什么 AlarmOnScreen Activity 沒有啟動? 為什么 Main Activity(第 511-514 行)已經開始? 最近我讓 AlarmOnScreen 在清單中設置了單任務模式。 這可能是導致問題的原因嗎? 但為什么? 由於其他幾個原因,我需要這個 singleTask ......
設備為 GT-S5830i,Android 2.3.6。
我仍然不知道是什么導致了這個問題,但如果有人會遇到類似的問題——經過多次測試,這只是一個簡單的改變——在 StartAlarmReceiver 中將 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 添加到 Intent。
所以就像
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
它有效嗎? 是的,它確實。 到目前為止,它沒有在其他手機上造成任何問題。
是否有意義? 一點都不。
我通過這種方式發送通知解決了我的服務問題:
startForeground(ID, notification)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.