Android gets activity (app) status and other information

Source: Internet
Author: User


detects whether an activity is at the top of the current task's stack
public static Boolean Istopactivy (String cmdName, Context context)    {        Activitymanager manager = (Activitymanager ) Context.getsystemservice (context.activity_service);        list<runningtaskinfo> Runningtaskinfos = Manager.getrunningtasks (integer.max_value);        String cmpnametemp = null;        if (null! = Runningtaskinfos)        {            cmpnametemp = (runningtaskinfos.get (0). topactivity). toString ()        ;        if (null = = Cmpnametemp)        {            return false;        }                Return Cmpnametemp.equals (CmdName);    }


determine if the Android app is in the foreground
public static Boolean Isapponforeground (context context)    {        Activitymanager Activitymanager = (activitymanager ) Context.getsystemservice (context.activity_service);        String PackageName = Context.getpackagename ();        list<recenttaskinfo> apptask = Activitymanager.getrecenttasks (Integer.max_value, 1);                if (Apptask = = null)        {            return false;        }                if (apptask.get (0). Baseintent.tostring (). Contains (PackageName))        {            return true;        }        return false;    }


get all the desktops installed in your Android phone
private static list<string> Getallthelauncher (context context)    {        list<string> names = null;        Packagemanager PKGMGT = Context.getpackagemanager ();        Intent it = new Intent (intent.action_main);        It.addcategory (intent.category_home);        List<resolveinfo> RA = pkgmgt.queryintentactivities (it, 0);        if (ra.size ()! = 0)        {            names = new arraylist<string> ();        }        for (int i = 0; i < ra.size (); i++)        {            String packagename = Ra.get (i). Activityinfo.packagename;            Names.add (PackageName);        }        return names;    }

Android Judging program front and back status
public static Boolean Islauncherrunnig (context context)    {        Boolean result = false;        list<string> names = Getallthelauncher (context);        Activitymanager Mactivitymanager = (activitymanager) context.getsystemservice (context.activity_service);        list<activitymanager.runningappprocessinfo> applist = mactivitymanager.getrunningappprocesses ();        for (Runningappprocessinfo running:applist)        {            if (running.importance = = Runningappprocessinfo.importance_ FOREGROUND)            {for                (int i = 0; i < names.size (); i++)                {                    if (names.get (i). Equals (Running.processname )                    {                        result = true;                        Break        ;        }}}} return result;    }


Android gets activity (app) status and other information

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.