似乎有一些與主題相關的問題,但我還沒有找到明確的是/否答案。
我有一個呼叫setExactAndAllowWhileIdle以啟動廣播服務的前臺服務。下面是廣播接收器中的代碼。
public class StepCountUpdaterAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myExampleApp::UpdateSteps");
wakeLock.acquire(3);
StepCounterHandler handler = StepCounterHandler.getInstance();
new TaskRunner().executeAsync(
new SetStepsForDay(SaveSharedPreference.getMemberId(context),
handler.getCumulativeSteps()),result -> {
handler.setAlarm(context);
if(isTimeToReset())
handler.resetStepsCounter();
});
wakeLock.release();
}
在setExactAndAllowWhileIdle 檔案中它指出:
發出警報后,該應用程式還將添加到系統的臨時電源豁免串列中大約 10 秒,以允許該應用程式獲取更多喚醒鎖以完成其作業。
但在打盹模式檔案中,它將此宣告為限制:
系統忽略喚醒鎖。
Does that mean acquiring a partial wake lock for 3 minutes in the 10 second window provided by an alarm dispatched by setExactAndAllowWhileIdle in Doze mode will effectively be useless or will it work fine?
In my case, the Broadcast Receiver will send data to my remote server via that async task, and after that it will set the alarm again and reset my steps counter. Will this work and if it wont, what are my alternatives for sending a network request in doze mode and executing follow up code?
EDIT: Testing by debugging my app shows that when forcing a device into idle mode, i still have network access and can send data to my server. The Doze mode documentation states how to force to app into the idle state which i am fairly sure is synonymous with doze mode. Yet this is supposed to be a restriction so I am clueless as to how this could be working.
uj5u.com熱心網友回復:
是的,Doze 會忽略你的喚醒鎖。但是,使用 setExactAndAllowWhile Idle,您將在正確的時間進行作業,并且您將擁有 10 秒的視窗來執行您希望的任何處理。
uj5u.com熱心網友回復:
如果設備處于打盹模式, AWakeLock對您沒有多大幫助。如果您已使用或作為鬧鐘,setExactAndAllowWhileIdle將喚醒您的設備。AlarmManager.ELAPSED_REALTIME_WAKEUPAlarmManager.RTC_WAKEUPtype
但是,從 Android 12 開始,您需要SCHEDULE_EXACT_ALARM獲得設定確切鬧鐘的權限。如果您打算將應用程式發布到 PlayStore,則可以使用一些可接受的用例來設定準確的警報。確保您的應用符合這些政策。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446614.html
