android实现获取系统全局对象实例

无需Context获取系统常用全局对象:Application,Activity,PackageManager等。

java 复制代码
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取系统全局实例对象,无需Context
 */
public class GlobalHelper {

    /**
     * 获取当前应用实例
     *
     * @return
     */
    public static Application getApplication() {
        try {
            Class ActivityThread = Class.forName("android.app.ActivityThread");
            Method currentApplication = ActivityThread.getMethod("currentApplication");
            currentApplication.setAccessible(true);
            return (Application) currentApplication.invoke(null);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取UI线程
     *
     * @return
     */
    public static Object getActivityThread() {
        try {
            Class ActivityThread = Class.forName("android.app.ActivityThread");
            Method currentActivityThread = ActivityThread.getMethod("currentActivityThread");
            currentActivityThread.setAccessible(true);
            return currentActivityThread.invoke(null);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取主线程Handler
     * @return
     */
    public static Handler getMainHandler() {
        try {
            Class ActivityThread = Class.forName("android.app.ActivityThread");
            Field sMainThreadHandlerField = ActivityThread.getDeclaredField("sMainThreadHandler");
            sMainThreadHandlerField.setAccessible(true);
            return (Handler) sMainThreadHandlerField.get(null);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取系统上下文实例ContextImpl
     *
     * @return
     */
    public static Context getSystemContext() {
        Object mActivityThread = getActivityThread();
        if (mActivityThread != null) {
            try {
                Method getSystemContext = mActivityThread.getClass().getDeclaredMethod("getSystemContext");
                getSystemContext.setAccessible(true);
                return (Context) getSystemContext.invoke(mActivityThread);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取全局上下文
     *
     * @return
     */
    public static Context getContext() {
        Context context = getApplication();
        if (context != null) {
            return context;
        } else {
            return getSystemContext();
        }
    }


    /**
     * 获取包管理器对象
     *
     * @return
     */
    public static PackageManager getPackageManager() {
        Context context = getContext();
        if (context != null) {
            return context.getPackageManager();
        } else {
            return null;
        }
    }

    /**
     * 获取包管理器接口
     *
     * @return
     */
    public static Object getIPackageManager() {
        try {
            Class ActivityThread = Class.forName("android.app.ActivityThread");
            Method getPackageManager = ActivityThread.getMethod("getPackageManager");
            getPackageManager.setAccessible(true);
            return getPackageManager.invoke(null);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建包管理器接口实例
     *
     * @return
     */
    public static IInterface newIPackageManager() {
        try {
            Class ServiceManager = Class.forName("android.os.ServiceManager");
            Method getService = ServiceManager.getMethod("getService", String.class);
            getService.setAccessible(true);
            IBinder mIBinder = (IBinder) getService.invoke(ServiceManager, "package");
            Class<?> IPackageManagerStub = Class.forName("android.content.pm.IPackageManager$Stub");
            Method asInterface = IPackageManagerStub.getDeclaredMethod("asInterface", IBinder.class);
            asInterface.setAccessible(true);
            return (IInterface) asInterface.invoke(null, mIBinder);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取所有Activity实例
     *
     * @return
     */
    public static List<Activity> getActivities() {
        List<Activity> activities = new ArrayList<>();
        Object mActivityThread = getActivityThread();
        try {
            Class mActivityThreadClass = mActivityThread.getClass();
            Field mActivitiesField = mActivityThreadClass.getDeclaredField("mActivities");
            mActivitiesField.setAccessible(true);
            Object mActivities = mActivitiesField.get(mActivityThread);
            if (mActivities instanceof Map) {
                Map<IBinder, Object> arrayMap = (Map) mActivities;
                for (Map.Entry<IBinder, Object> entry : arrayMap.entrySet()) {
                    Object mActivityClientRecord = entry.getValue();
                    if (mActivityClientRecord != null) {
                        Field activityField = mActivityClientRecord.getClass().getDeclaredField("activity");
                        activityField.setAccessible(true);
                        Activity activity = (Activity) activityField.get(mActivityClientRecord);
                        if (activity != null) {
                            activities.add(activity);
                        }
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return activities;
    }

    /**
     * 获取所有服务实例
     *
     * @return
     */
    public static List<Service> getServices() {
        List<Service> services = new ArrayList<>();
        Object mActivityThread = getActivityThread();
        try {
            Class mActivityThreadClass = mActivityThread.getClass();
            Field mServicesField = mActivityThreadClass.getDeclaredField("mServices");
            mServicesField.setAccessible(true);
            Object mServices = mServicesField.get(mActivityThread);
            if (mServices instanceof Map) {
                Map<IBinder, Service> arrayMap = (Map) mServices;
                for (Map.Entry<IBinder, Service> entry : arrayMap.entrySet()) {
                    Service service = entry.getValue();
                    if (service != null) {
                        services.add(service);
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return services;
    }


}
相关推荐
Dnelic-2 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen4 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年12 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿14 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神15 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛16 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法16 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter17 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快18 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl19 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5