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;
    }


}
相关推荐
mmsx12 分钟前
android sqlite 数据库简单封装示例(java)
android·java·数据库
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
吃着火锅x唱着歌4 小时前
PHP7内核剖析 学习笔记 第四章 内存管理(1)
android·笔记·学习
_Shirley5 小时前
鸿蒙设置app更新跳转华为市场
android·华为·kotlin·harmonyos·鸿蒙
hedalei7 小时前
RK3576 Android14编译OTA包提示java.lang.UnsupportedClassVersionError问题
android·android14·rk3576
锋风Fengfeng7 小时前
安卓多渠道apk配置不同签名
android
枫_feng8 小时前
AOSP开发环境配置
android·安卓
胡西风_foxww8 小时前
【es6复习笔记】生成器(11)
javascript·笔记·es6·实例·生成器·函数·gen
叶羽西8 小时前
Android Studio打开一个外部的Android app程序
android·ide·android studio
qq_171538859 小时前
利用Spring Cloud Gateway Predicate优化微服务路由策略
android·javascript·微服务