When writing an Android application, the interface is usually related to the activity, many times we need to use the activity object in the pure Java environment, in general we all rely on parameter pass to use. Now we can use the pure Java code to get the activity object in the current system, with the activity object we can do a lot of things we want to do, such as monitoring the life cycle of the activity, to change the activity interface to add floating view and so on. Cut the crap, look at the following methods:
public static Activity getglobleactivity () throws ClassNotFoundException, IllegalArgumentException, SecurityException , Illegalaccessexception, InvocationTargetException, nosuchmethodexception, nosuchfieldexception{Class activityThr Eadclass = Class.forName ("Android.app.ActivityThread"); Object Activitythread = Activitythreadclass.getmethod ("Currentactivitythread"). Invoke (null); Field Activitiesfield = Activitythreadclass.getdeclaredfield ("mactivities"); Activitiesfield.setaccessible (TRUE); Map activities = (map) 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 ("A Ctivity "); ActivitYfield.setaccessible (TRUE); Activity activity = (activity) activityfield.get (Activityrecord); return activity; }} return null; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android gets current interface activity object