public static String getTopActivityName() {
long start = System.currentTimeMillis();
try {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);
Map<Object, Object> activities;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
activities = (HashMap<Object, Object>) activitiesField.get(activityThread);
} else {
activities = (ArrayMap<Object, Object>) activitiesField.get(activityThread);
}
if (activities.size() < 1) {
return null;
}
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.getClass().getName();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
long cost = System.currentTimeMillis() - start;
MatrixLog.d(TAG, "[getTopActivityName] Cost:%s", cost);
}
return null;
}
网友评论