měl bych obecný dotaz k plánovaným (časovaným) operacím na Androidu z aplikace.
Začínám tak, že si odvodím třídu z BroadcastReceiveru
- Kód: Vybrat vše
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Zde provedu akci, která se má provést při plánování
}
}
Na tuto třídu použiji referenci v mojí aktivitě, třeba takto:
- Kód: Vybrat vše
public void onClickBtnClick(View v) // Při kliknutí na buttonek naplánuj:
{
AlarmManager alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, 20000000, pendingIntent);
}
Ovšem někdy se to nechová tak, jak by mělo.
Obecně jsem vypozoroval, že při krátkých, několikaminutových intervalech
to funguje docela spolehlivě. Ovšem pokud tam zadám interval v řádu hodin, tak většinou žádné samočinné upozornění neproběhne. Upozornění proběhne však okamžitě, jakmile mobil probudím.
Všimnul jsem si, že podobně nespolehlivě se chovají i profesionální aplikace na GooglePlay.
Například Bluemail a podobně.
Mám mobil Asus Zenfone Max Pro M1 ZB602KL.
Potřeboval bych se prosím zeptat, zda tento problém souvisí obecně s agresivním uspáváním na mobilech, nebo zda jsem něco přehlédl.
Co jsem četl na internetu, tak u nových systémů se doporučuje spíše používat jinou třídu namísto AlarmManager.
Na straně druhé je internet plný podobných příspěvků, kdy se lidem nechce alarm aktivovat a v podstatě tam nejsou kloudné odpovědi. Respektive toto jsem odkoukal, jako "best practise" postup.
Každý android obsahuje aplikaci budík, která funguje spolehlivě.
A pak je otázkou zda je taková aplikace nějak privilegovaná nebo jen dobře napsaná.
Tak zdá se, tady jsem našel smutnou odpověď, přímo v dokumentaci:
Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.
With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, android.app.PendingIntent) and setExact(int, long, android.app.PendingIntent).
Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact.