通過Activity線程獲取Application/Context

public static Application getApplicationInner() {
    try {
        Class<?> activityThread = Class.forName("android.app.ActivityThread");

        Method currentApplication = activityThread.getDeclaredMethod("currentApplication");
        Method currentActivityThread = activityThread.getDeclaredMethod("currentActivityThread");

        Object current = currentActivityThread.invoke((Object)null);
        Object app = currentApplication.invoke(current);

        return (Application)app;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章