Android gets current interface activity object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.