Android获取所有Activity

Posted 早起的年轻人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android获取所有Activity相关的知识,希望对你有一定的参考价值。

1 方式一

    public void getAllActivity() 
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = null;
        try 
            packageInfo = packageManager.getPackageInfo(
                    getPackageName(), PackageManager.GET_ACTIVITIES);
            //所有的Activity 
            ActivityInfo[] activities = packageInfo.activities;
            for (ActivityInfo activity :activities ) 
                Class<?> aClass = Class.forName(activity.name);
            

         catch (PackageManager.NameNotFoundException | ClassNotFoundException e) 
            e.printStackTrace();
        
    

2 方式二 反射

    private static List<Activity> getActivitiesByApplication(Application application) 
        List<Activity> list = new ArrayList<>();
        try 
            Class<Application> applicationClass = Application.class;
            Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
            mLoadedApkField.setAccessible(true);
            Object mLoadedApk = mLoadedApkField.get(application);
            Class<?> mLoadedApkClass = mLoadedApk.getClass();
            Field mActivityThreadField = mLoadedApkClass.getDeclaredField("mActivityThread");
            mActivityThreadField.setAccessible(true);
            Object mActivityThread = mActivityThreadField.get(mLoadedApk);
            Class<?> mActivityThreadClass = mActivityThread.getClass();
            Field mActivitiesField = mActivityThreadClass.getDeclaredField("mActivities");
            mActivitiesField.setAccessible(true);
            Object mActivities = mActivitiesField.get(mActivityThread);
            // 注意这里一定写成Map,低版本这里用的是HashMap,高版本用的是ArrayMap
            if (mActivities instanceof Map) 
                @SuppressWarnings("unchecked")
                Map<Object, Object> arrayMap = (Map<Object, Object>) mActivities;
                for (Map.Entry<Object, Object> entry : arrayMap.entrySet()) 
                    Object value = entry.getValue();
                    Class<?> activityClientRecordClass = value.getClass();
                    Field activityField = activityClientRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    Object o = activityField.get(value);
                    list.add((Activity) o);
                
            
         catch (Exception e) 
            e.printStackTrace();
            list = null;
        
        return list;
    

3 获取栈顶 Activity

    //获取栈顶Activity及其所属进程
    public static String getTopActivityNameAndProcessName(Context context) 
        String processName = null;
        String topActivityName = null;
        ActivityManager activityManager =
                (ActivityManager) (context.getSystemService(android.content.Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
        if (runningTaskInfos != null) 
            ActivityManager.RunningTaskInfo runningTaskInfo = runningTaskInfos.get(0);
            ComponentName f = runningTaskInfo.topActivity;
            String topActivityClassName = f.getClassName();
            String temp[] = topActivityClassName.split("\\\\.");
            //栈顶Activity的名称
            topActivityName = temp[temp.length - 1];
            int index = topActivityClassName.lastIndexOf(".");
            //栈顶Activity所属进程的名称
            processName = topActivityClassName.substring(0, index);
            System.out.println("---->topActivityName=" + topActivityName + ",processName=" + processName);

        
        return topActivityName;
    

以上是关于Android获取所有Activity的主要内容,如果未能解决你的问题,请参考以下文章

Android获取所有Activity

IOC 控制反转Android 事件依赖注入 ( 事件依赖注入具体的操作细节 | 获取 Activity 中的所有方法 | 获取方法上的注解 | 获取注解上的注解 | 通过注解属性获取事件信息 )(代

Android获取启动页面Activity方法

Android获取启动页面Activity方法

Appium Android 获取包名和 Activity 的几种方法 (转)

Android——Activity恢复用户用EditText输入的数据