标签:s上下文
获取当前上下文,用于显示dialog或弹出toast public static Activity getGlobleActivity(){ Class<?> activityThreadClass; try { activityThreadClass = Class.forName("android.app.ActivityThread"); Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null); Field activitiesField = activityThreadClass.getDeclaredField("mActivities"); activitiesField.setAccessible(true); Map<?, ?> activities = (Map<?, ?>) activitiesField.get(activityThread); for(Object activityRecord:activities.values()){ Class<? extends Object> activityRecordClass = activityRecord.getClass(); Field pausedField = activityRecordClass.getDeclaredField("paused"); pausedField.setAccessible(true); if(!pausedField.getBoolean(activityRecord)) { Field activityField = activityRecordClass.getDeclaredField("activity"); activityField.setAccessible(true); Activity activity = (Activity) activityField.get(activityRecord); return activity; } } } catch (Exception e) { e.printStackTrace(); } return null; }
本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1769781
标签:s上下文
原文地址:http://670176656.blog.51cto.com/4500575/1769781