Tencent/matrix · error · IllegalStateException
NOT initialized yet
Error message
NOT initialized yet
What it means
ForegroundServiceLifecycleOwner.hasForegroundService() queries ActivityManager.getRunningServices to detect a foreground service in this process. If the plugin has not been initialized (activityManager is null), the query cannot run, so it throws IllegalStateException('NOT initialized yet').
Solutions
- Initialize Matrix (which wires up the foreground-service plugin) before calling hasForegroundService().
- Guard the call: only invoke it after Matrix.with(...) / plugin installation completes.
- Verify you are in the same process where the plugin is initialized.
Example fix
// before val has = ForegroundServiceLifecycleOwner.hasForegroundService() // after if (!Matrix.isInitialized()) return val has = ForegroundServiceLifecycleOwner.hasForegroundService()
Defensive patterns
Strategy: validation
Validate before calling
fun canQueryForegroundService() = Matrix.isInitialized()
Type guard
null
Try / catch
val has = try {
ForegroundServiceLifecycleOwner.hasForegroundService()
} catch (e: IllegalStateException) {
MatrixLog.w(TAG, "plugin not initialized", e)
false
} Prevention
- Call this only after Matrix init completes in Application.onCreate.
- Check the same process where the plugin was installed.
- Gate the call behind your app's 'Matrix ready' flag.
When it happens
Trigger: Calling hasForegroundService() before ForegroundServiceLifecycleOwner's onPluginServiceCreated/init has been invoked with an ActivityManager, typically before Matrix is started in the Application.
Common situations: Calling the API very early in Application.onCreate before Matrix init, or from another process / unit test where the plugin never initializes.
Related errors
- NOT initialized yet
- NOT allow to observe with DESTROYED lifecycle owner
- Supervisor NOT initialized yet or Supervisor is disabled!!!
- not initialized yet !
- plugin start, but plugin has been already destroyed
AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08).
Data as JSON: /api/errors/31e6350931f974e2.
Report an issue: GitHub.
Appendix: source
Thrown at matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/lifecycle/owners/ForegroundServiceLifecycleOwner.kt:88
ActivityThreadmServices = clazzActivityThread.getDeclaredField("mServices").let {
it.isAccessible = true
it.get(activityThread) as ArrayMap<*, *>?
}
ActivityThreadmH = fieldActivityThreadmH.get(activityThread) as Handler
Handler::class.java.getDeclaredField("mCallback").apply {
isAccessible = true
val origin = get(ActivityThreadmH) as Handler.Callback?
set(ActivityThreadmH, HHCallback(origin))
MatrixLog.i(TAG, "origin is ${origin?.javaClass?.name}")
}
}
}
fun hasForegroundService(): Boolean {
if (activityManager == null) {
throw IllegalStateException("NOT initialized yet")
}
return safeLet(TAG, defVal = false) {
@Suppress("DEPRECATION")
activityManager!!.getRunningServices(Int.MAX_VALUE)
.filter {
it.uid == Process.myUid() && it.pid == Process.myPid()
}.any {
it.foreground
}
}.also {
if (!it) {
fgServiceHandler?.clear()
}
}
}
private class HHCallback(private val mHCallback: Handler.Callback?) : Handler.Callback {
View on GitHub (pinned to 3b8293bd65)