MCPcopy Create free account
hub / github.com/afkT/DevUtils / getApplicationByReflect

Method getApplicationByReflect

lib/DevApp/src/main/java/dev/DevUtils.java:227–242  ·  view source on GitHub ↗

通过 ActivityThread 反射获取 Application 失败或未就绪时返回 null,不在方法边界抛出未捕获异常。 @return Application

()

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 4

eTagMethod · 0.95
forNameMethod · 0.45
invokeMethod · 0.45
getMethodMethod · 0.45

Tested by

no test coverage detected