android 项目,把一个子模块打成 jar 包,用 adb 命令执行 jar 包。但是代码中获取到 application 都是为空
是因为 apply plugin: 'com.andoid.library'这个的缘故吗?
获取代码如下 private static Application currentApplication;
/** * 获取全局的 application * * @return 返回 application */ @SuppressLint("PrivateApi") public static Application getNewApplication() { try { if (currentApplication == null) { currentApplication = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null, (Object[]) null); } return currentApplication; } catch (Exception e) { e.printStackTrace(); } return null; }
![]() | 1 tanranran 2022-10-31 21:51:27 +08:00 大哥,系统性过一下 Androi 的基本知识吧 |
![]() | 2 ikas 2022-10-31 21:51:43 +08:00 获取 context 足够 public static Context getLimitedSystemContext() { var currentActivityThread = ActivityThread.currentActivityThread(); if (null == currentActivityThread) { synchronized (ActivityThread.class) { currentActivityThread = ActivityThread.currentActivityThread(); if (null == currentActivityThread) { if (Looper.getMainLooper() == null) { Looper.prepareMainLooper(); } currentActivityThread = ActivityThread.systemMain(); } } } var limitedSystemCOntext= (Context) currentActivityThread.getSystemContext(); return limitedSystemContext; } |