获取Activity

2016-10-25  本文已影响132人  chandarlee
    /**
     * 
     * @param context Application
     * @param foregroundOnly
     * @return
     */
    public static List<Activity> getActivities(Context context, boolean foregroundOnly) {
        List list = new ArrayList();
        try {
            Class activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = getActivityThread(context, activityThreadClass);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);

            Object collection = activitiesField.get(activityThread);
            Collection c;
            if (collection instanceof HashMap){
                c = ((HashMap)collection).values();
            } else {
                if ((Build.VERSION.SDK_INT >= 19) && (collection instanceof android.util.ArrayMap)){
                    android.util.ArrayMap activities = (android.util.ArrayMap)collection;
                    c = activities.values();
                } else {
                    return list;
                }
            }
            for (Iterator activities = c.iterator(); activities.hasNext(); ) {
                Object activityRecord = activities.next();
                Class activityRecordClass = activityRecord.getClass();
                if (foregroundOnly) {
                    Field pausedField = activityRecordClass.getDeclaredField("paused");
                    pausedField.setAccessible(true);
                    if (pausedField.getBoolean(activityRecord)) {
                        continue;
                    }
                }
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                Activity activity = (Activity) activityField.get(activityRecord);
                if (activity != null) {
                    list.add(activity);
                }
            }
        }catch (Throwable localThrowable){
            localThrowable.printStackTrace();
        }
        return list;
    }

    public static Object getActivityThread(Context context, Class<?> activityThread)
    {
        try
        {
            if (activityThread == null) {
                activityThread = Class.forName("android.app.ActivityThread");
            }
            Method m = activityThread.getMethod("currentActivityThread", new Class[0]);
            m.setAccessible(true);
            Object currentActivityThread = m.invoke(null, new Object[0]);
            if ((currentActivityThread == null) && (context != null))
            {
                //反射访问Application中的mLoadedApk,所以这里的context需要为Application实例
                //可通过getApplicationContext()获取
                Field mLoadedApk = context.getClass().getField("mLoadedApk");
                mLoadedApk.setAccessible(true);
                Object apk = mLoadedApk.get(context);
                Field mActivityThreadField = apk.getClass().getDeclaredField("mActivityThread");
                mActivityThreadField.setAccessible(true);
                currentActivityThread = mActivityThreadField.get(apk);
            }
            return currentActivityThread;
        } catch (Throwable ignore) {
            ignore.printStackTrace();
        }
        return null;
    }
上一篇下一篇

猜你喜欢

热点阅读