通过 ActivityThread 反射获取 Application 失败或未就绪时返回 null,不在方法边界抛出未捕获异常。 @return Application
()
| 225 | * @return {@link Application} |
| 226 | */ |
| 227 | private static Application getApplicationByReflect() { |
| 228 | try { |
| 229 | @SuppressLint("PrivateApi") |
| 230 | Class<?> activityThread = Class.forName("android.app.ActivityThread"); |
| 231 | Object thread = activityThread.getMethod("currentActivityThread").invoke(null); |
| 232 | Object app = activityThread.getMethod("getApplication").invoke(thread); |
| 233 | if (app == null) { |
| 234 | LogPrintUtils.eTag(TAG, "getApplicationByReflect: application is null"); |
| 235 | return null; |
| 236 | } |
| 237 | return (Application) app; |
| 238 | } catch (Exception e) { |
| 239 | LogPrintUtils.eTag(TAG, e, "getApplicationByReflect"); |
| 240 | } |
| 241 | return null; |
| 242 | } |
| 243 | |
| 244 | // = |
| 245 |
no test coverage detected