'BroadcastReceiver in Android 12+
a BroadcastReceiver (TIME_TICK,USER_PRESENT) needs a Long running Service that no stopped at a time or when close a Activity (example a Widget)
Since Android 12 a Service start not with ForegroundServiceStartNotAllowedException.
But WorkManager have setForeground(). Is this also a Service?
How can i start with WorkManager and CoroutineWorker a Service that Register the BroadcastReceiver for long running?
What means this in Manifest.xml?
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="location|microphone"
tools:node="merge" />
Solution 1:[1]
But WorkManager have setForeground(). Is this also a Service?
Yes, under the hood WorkManger uses its own Service.
The idea of the WorkManager is for you to have some work that you need to do and some expectations(restrictions like Network, Battery, etc) when it should be executed. It is not designed to raise another Service from it. If Android is preventing you from starting a Service from the Background the point is for you to think over your design and not to try to hack it by starting it from another API.
Please provide more info about your use case.
Solution 2:[2]
My Use Case
Refresh a clock with a minute offset with TIME_TICK.
Call Weather Data when the user unlocked the Phone.
Refresh the Widgetview with a Broadcast Message from a Setting Activity.
Other Solution
"TextClock" but have not a flexible Offset
Call Weather Data every 30 minutes with a Work Manager
How can i refresh a View in MainActitivity from a other Actitvity without Broadcast Send?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yavor Mitev |
| Solution 2 |
