I have a background job in a WearOS app that calls an API, updates a Work database, and notifies my tile and complications about the new data.
Sometimes the background job runs, but the network is not available. Here is my relevant WorkManager code:
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.build()
val repeatingRequest = PeriodicWorkRequestBuilder<RefreshDataWorker>(20, TimeUnit.MINUTES)
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
Log.d("WorkManager", "Periodic Work request for sync is scheduled")
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
RefreshDataWorker.SCHEDULED_WORK_NAME,
ExistingPeriodicWorkPolicy.UPDATE,
repeatingRequest)
I see Logcat messages like below. It seems like the job gets scheduled, the system realizes the network is down, but then immediately indicates that the network is up. However, when the job executes, the network is actually down.
WM-DelayedWorkTracker com.mypackage D Scheduling work da88277f-b68e-4ab4-90d3-08673582546c
WM-GreedyScheduler com.mypackage D Starting tracking for da88277f-b68e-4ab4-90d3-08673582546c
WM-ConstraintTracker com.mypackage D a: initial state = true
WM-BrdcstRcvrCnstrntTrc com.mypackage D a: registering receiver
WM-ConstraintTracker com.mypackage D i: initial state = NetworkState(isConnected=false, isValidated=false, isMetered=true, isNotRoaming=true)
WM-NetworkStateTracker com.mypackage D Registering network callback
WM-GreedyScheduler com.mypackage D Constraints not met: Cancelling work ID WorkGenerationalId(workSpecId=da88277f-b68e-4ab4-90d3-08673582546c, generation=408)
WM-NetworkStateTracker com.mypackage D Network capabilities changed: [ Transports: WIFI Capabilities: NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED&NOT_VCN_MANAGED LinkUpBandwidth>=9728Kbps LinkDnBandwidth>=37063Kbps TransportInfo: <SSID: <unknown ssid>, BSSID: 02:00:00:00:00:00, MAC: 02:00:00:00:00:00, IP: /192.168.1.101, Security type: 2, Supplicant state: COMPLETED, Wi-Fi standard: 11n, RSSI: -53, Link speed: 72Mbps, Tx Link speed: 72Mbps, Max Supported Tx Link speed: 150Mbps, Rx Link speed: 72Mbps, Max Supported Rx Link speed: 150Mbps, Frequency: 5745MHz, Net ID: -1, Metered hint: false, score: 60, isUsable: true, CarrierMerged: false, SubscriptionId: -1, IsPrimary: -1, Trusted: true, Restricted: false, Ephemeral: false, OEM paid: false, OEM private: false, OSU AP: false, FQDN: <none>, Provider friendly name: <none>, Requesting package name: <none><none>MLO Information: , Is TID-To-Link negotiation supported by the AP: false, AP MLD Address: <none>, AP MLO Link Id: <none>, AP MLO Affiliated links: <none>, Vendor Data: <none>> SignalStrength: -53 UnderlyingNetworks: Null]
WM-GreedyScheduler com.mypackage D Constraints met: Scheduling work ID WorkGenerationalId(workSpecId=da88277f-b68e-4ab4-90d3-08673582546c, generation=408)
WM-Processor com.mypackage D e: processing WorkGenerationalId(workSpecId=da88277f-b68e-4ab4-90d3-08673582546c, generation=408)
WM-WorkerWrapper com.mypackage D Starting work for com.mypackage.work.RefreshDataWorker
RefreshDataWorker com.mypackage I No active network.
Repository com.mypackage E Failed to refresh data
Repository com.mypackage I Exception info: java.net.UnknownHostException: Unable to resolve host "<address>": No address associated with hostname
Message: Unable to resolve host "<address>": No address associated with hostname
Cause: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) Class: class java.net.UnknownHostException
Any ideas what might be going on here?