static boolean isMainProcess(Context context) { try { Class<?> clazz = Class.forName("android.app.ActivityThread"); Method currentActivityThreadMethod = clazz.getDeclaredMethod("currentActivityThread"); currentActivityThreadMethod.setAccessible(true); Object tCurrentActivityThread = currentActivityThreadMethod.invoke(null); Method tGetProcessNameMethod = clazz.getDeclaredMethod("getProcessName"); tGetProcessNameMethod.setAccessible(true); String tProcessName = (String) tGetProcessNameMethod.invoke(tCurrentActivityThread); return context.getPackageName().equalsIgnoreCase(tProcessName); } catch (Throwable e) { return true; } }
转载于:https://my.oschina.net/sfshine/blog/2996905