美文网首页
Android 6.0 以上获取正在运行的Activity信息反

Android 6.0 以上获取正在运行的Activity信息反

作者: zhangxuanchen | 来源:发表于2017-02-14 11:10 被阅读451次
import android.app.Application;
import android.app.Instrumentation;
import android.content.Context;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;


public class AppUtils {

    public static void getNowActivity(Application app){
        Field f;
        Object thread = null;
        Instrumentation base;
        // Replace instrumentation
        try {
            thread = getActivityThread(app);
            f = thread.getClass().getDeclaredField("mInstrumentation");
            f.setAccessible(true);
            base = (Instrumentation) f.get(thread);
            if(base != null){
                  //get instrumentation activity list
                String name;
                Object value;
                List<Instrumentation.ActivityMonitor> mActivityMonitors;
                Field[] fields = Instrumentation.class.getClass().getDeclaredFields();
                Field.setAccessible(fields, true);
                for (int i = 0; i < fields.length;   i++)   {
                    name = fields[i].getName();
                    value = fields[i].get(f);
                    if(name != null && name.equals("mActivityMonitors")) {
                        mActivityMonitors = (List<Instrumentation.ActivityMonitor>) value;
                        for(int j = 0 ; j < mActivityMonitors.size() ; j ++){
                            Instrumentation.ActivityMonitor activityMonitor = mActivityMonitors.get(i);
                            //获取Activity名称
                            activityMonitor.getLastActivity().getClass().getName();
                        }
                        return;
                    }
                }               
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to replace instrumentation for thread: " + thread);
        }
    }

    private static Object getActivityThread(Context context) {
        try {
            Class activityThread = Class.forName("android.app.ActivityThread");
            // ActivityThread.currentActivityThread()
            Method m = activityThread.getMethod("currentActivityThread", new Class[0]);
            m.setAccessible(true);
            Object thread = m.invoke(null, new Object[0]);
            if (thread != null) return thread;

            // context.@mLoadedApk.@mActivityThread
            Field mLoadedApk = context.getClass().getField("mLoadedApk");
            mLoadedApk.setAccessible(true);
            Object apk = mLoadedApk.get(context);
            Field mActivityThreadField = apk.getClass().getDeclaredField("mActivityThread");
            mActivityThreadField.setAccessible(true);
            return mActivityThreadField.get(apk);
        } catch (Throwable ignore) {
            throw new RuntimeException("Failed to get mActivityThread from context: " + context);
        }
    }
}

相关文章

网友评论

      本文标题:Android 6.0 以上获取正在运行的Activity信息反

      本文链接:https://www.haomeiwen.com/subject/norxwttx.html