How Android can tell if an app is running

Source: Internet
Author: User

In one application, or in a service, receiver, it is sometimes necessary to determine whether an application is running in order to perform some related processing . this time we need to get a activitymanager, this manager as implies is to manage the activity, it has a method called Getrunningtasks, you can get the current system is running a list of tasks, the code is as follows:

Activitymanager am = (activitymanager) Context.getsystemservice (Context.activity_service); List<RunningTaskInfo> list = am.getrunningtasks (  runningtaskinfo info:list) {     if (Info.topActivity.getPackageName (). Equals (my_pkg_name) &&  Info.baseActivity.getPackageName (). Equals (My_pkg_name)) {         true;          //  break        ;     }}  

100 represents the maximum number of tasks taken, info.topactivity indicates that the current running Activity,info.baseactivity table system background has this process running, specifically to do how to judge the business needs to see itself. This class also has more methods to get the system running services, memory usage, etc., please find out for yourselves.

One thing to note is that if you want to run this method correctly, add the following in your androidmanifest.xml:

<uses-permission android:name= "Android.permission.GET_TASKS"/>  

/*** Determine if the app is running *@paramContext *@return     */     Public BooleanIsrun (Context context) {Activitymanager am=(Activitymanager) Context.getsystemservice (Context.activity_service); List<RunningTaskInfo> list = am.getrunningtasks (100); BooleanIsapprunning =false; String My_pkg_name= "Com.ad"; //100 represents the maximum number of tasks taken, info.topactivity indicates that the current running Activity,info.baseactivity table system background has this process running         for(Runningtaskinfo info:list) {if(Info.topActivity.getPackageName (). Equals (My_pkg_name) | |info.baseActivity.getPackageName (). Equals (My_pkg_name)) {isapprunning=true; LOG.I ("Activityservice Isrun ()", info.topActivity.getPackageName () + "info.baseActivity.getPackageName () =" +info.baseActivity.getPackageName ());  Break; }} log.i ("Activityservice Isrun ()", "Com.ad program ... isapprunning ..." +isapprunning); returnisapprunning; }

the relevant API for the internal status information of the Android system:
Get Activitymanager:

Activitymanager Activitymanager = (activitymanager) this.getsystemservice (Activity_service)

Configurationinfo configurationinfo = Activitymanager.getdeviceconfigurationinfo ();

Get information about the status of the process memory:

debug.memoryinfo[] Processmemoryinfo = Activitymanager.getprocessmemoryinfo (processids)

Get service information that is currently running:

list<runningserviceinfo> Runningserviceinfos = activitymanager.getrunningservices (MaxValue);

Gets the currently running task information:

list<runningtaskinfo> Runningtaskinfos = Activitymanager.getrunningtasks (MaxValue);

one of the Runningtaskinfos topactivity is the active activity of the current task. in the task queue returned by Getrunningtasks (), the system will have a sort according to the activity of these tasks, the more active the more forward. The first one is the currently active task

/*** Detects if a activityupdate is at the top of the current task's stack*/  Public BooleanIstopactivy (String cmdName) {Activitymanager Manager=(Activitymanager) Context.getsystemservice (Activity_service); List<RunningTaskInfo> Runningtaskinfos = manager.getrunningtasks (1); String cmpnametemp=NULL; if(NULL!=Runningtaskinfos) {Cmpnametemp= (Runningtaskinfos.get (0). topactivity). toString); LOG.E ("Cmpname", "Cmpname:" +cmpname); }          if(NULL= = cmpnametemp)return false; returncmpnametemp.equals (cmdName);} 

/**Get the launcher status*/       Private  BooleanIslauncherrunnig (Context context) {Booleanresult =false ; List<String> names =Getallthelauncher (); 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(inti = 0; I < names.size (); i++) {                                       if(Names.get (i). Equals (Running.processname)) {result=true ;  Break; }                               }                       }               }    returnresult; }  
/*** Get all the launcher*/PrivateList<string>Getallthelauncher () {List<String> names =NULL; Packagemanager PKGMGT= This. Getpackagemanager (); Intent it=NewIntent (Intent.action_main);            It.addcategory (Intent.category_home); List<ResolveInfo> RA =pkgmgt.queryintentactivities (it,0); if(Ra.size ()! = 0) {names=NewArraylist<string>(); }            for(inti=0;i< ra.size (); i++) {String PackageName=Ra.get (i). Activityinfo.packagename;           Names.add (PackageName); }             returnnames; } 

Android takes the activity currently displayed:

Activitymanager am == am.getrunningtasks (1). Get (0). topactivity; LOG.D ("", "pkg:" +cn.getpackagename ()); LOG.D (

How Android Determines if a program is running in the foreground:

Private Boolean istopactivity () {           List<RunningTaskInfo>  tasksinfo = activitymanager.getrunningtasks (1) ;     if (Tasksinfo.size () > 0) {               // The application is located on the   top level of the stack            if( Packagename.equals (tasksinfo.get (0). Topactivity.getpackagename ())) {                   returnTrue ;               }           }            return false ;       }  

How Android determines that an app is running

Related Article

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.