這一個對我的作品,最小的SDK水平是18
public static Activity getCurrentActivity(){
try {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);
ArrayMap activities = (ArrayMap) activitiesField.get(activityThread);
for (Object activityRecord : activities.values()) {
Class activityRecordClass = activityRecord.getClass();
Field pausedField = activityRecordClass.getDeclaredField("paused");
pausedField.setAccessible(true);
if (!pausedField.getBoolean(activityRecord)) {
Field activityField = activityRecordClass.getDeclaredField("activity");
activityField.setAccessible(true);
Activity activity = (Activity) activityField.get(activityRecord);
return activity;
}
}
}catch (Exception e){
logger.error(e.getMessage());
}
return null;
}
Can [that](http://stackoverflow.com/a/18115551/4762282)能幫助你嗎? – Ircover
@翻譯,是的。我會說我的問題是一個重複的問題。您是否應該將您的評論發佈爲我接受的答案?我不知道如何在這裏繼續,但你救了我的lfie =)。 –