通過反射獲取Application對象
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;
}
}
備註:可能由於Android系統版本的不一樣出現反射失敗;