{"record":{"id":"31e6350931f974e2","repo":"Tencent/matrix","slug":"not-initialized-yet","errorCode":null,"errorMessage":"NOT initialized yet","messagePattern":"NOT initialized yet","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/lifecycle/owners/ForegroundServiceLifecycleOwner.kt","lineNumber":88,"sourceCode":"            ActivityThreadmServices = clazzActivityThread.getDeclaredField(\"mServices\").let {\n                it.isAccessible = true\n                it.get(activityThread) as ArrayMap<*, *>?\n            }\n\n            ActivityThreadmH = fieldActivityThreadmH.get(activityThread) as Handler\n\n            Handler::class.java.getDeclaredField(\"mCallback\").apply {\n                    isAccessible = true\n                    val origin = get(ActivityThreadmH) as Handler.Callback?\n                    set(ActivityThreadmH, HHCallback(origin))\n                    MatrixLog.i(TAG, \"origin is ${origin?.javaClass?.name}\")\n                }\n        }\n    }\n\n    fun hasForegroundService(): Boolean {\n        if (activityManager == null) {\n            throw IllegalStateException(\"NOT initialized yet\")\n        }\n        return safeLet(TAG, defVal = false) {\n            @Suppress(\"DEPRECATION\")\n            activityManager!!.getRunningServices(Int.MAX_VALUE)\n                .filter {\n                    it.uid == Process.myUid() && it.pid == Process.myPid()\n                }.any {\n                    it.foreground\n                }\n        }.also {\n            if (!it) {\n                fgServiceHandler?.clear()\n            }\n        }\n    }\n\n    private class HHCallback(private val mHCallback: Handler.Callback?) : Handler.Callback {\n","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/lifecycle/owners/ForegroundServiceLifecycleOwner.kt#L70-L106","documentation":"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').","triggerScenarios":"Calling hasForegroundService() before ForegroundServiceLifecycleOwner's onPluginServiceCreated/init has been invoked with an ActivityManager, typically before Matrix is started in the Application.","commonSituations":"Calling the API very early in Application.onCreate before Matrix init, or from another process / unit test where the plugin never initializes.","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."],"exampleFix":"// before\nval has = ForegroundServiceLifecycleOwner.hasForegroundService()\n// after\nif (!Matrix.isInitialized()) return\nval has = ForegroundServiceLifecycleOwner.hasForegroundService()","handlingStrategy":"validation","validationCode":"fun canQueryForegroundService() = Matrix.isInitialized()","typeGuard":"null","tryCatchPattern":"val has = try {\n    ForegroundServiceLifecycleOwner.hasForegroundService()\n} catch (e: IllegalStateException) {\n    MatrixLog.w(TAG, \"plugin not initialized\", e)\n    false\n}","preventionTips":["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."],"tags":["android","lifecycle","initialization-order","illegal-state"],"backgroundTag":"module-init-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}