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