How Android can tell if an app is running

Source: Internet
Author: User

Android how to tell if an app is running   in an app, or a service, receiver to determine if an app is running for some related processing. This time we need to get a activitymanager, this manager as implies means to manage 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 (+);    for (runningtaskinfo info:list) {    &nbs P   if (Info.topActivity.getPackageName (). Equals (My_pkg_name) && info.baseActivity.getPackageName (). Equals (My_pkg_name)) {            isapprunning = true;          &NB Sp Find it, break            break;       }   }100 represents the maximum number of tasks to take, Info.topactivity indicates that the current running Activity,info.baseactivity table system background has this process running, specifically 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 "/> Otherwise there may be exception thrown. =====================================//determine if the app is running  activitymanager am = (activitymanager) Context.getsystemservice (Context.activity_service); list<runningtaskinfo> list = Am.getrunningtasks (+); Boolean isapprunning = false; String my_pkg_name = "Com.cyberblue.iitag"; for (Runningtaskinfo info:list) {if (Info.topActivity.getPackageName (). Equals (my_pkg_name) | | info.baseActivity.getPackageName (). Equals (my_pkg _name)) {isapprunning = true;log.i (Tag,info.topactivity.getpackagename () + "info.baseActivity.getPackageName () =" + Info.baseActivity.getPackageName ());Break ; }}//to restart Bluetooth when running, or it will cause Bluetooth not to close if (isapprunning) after installing this app {Bluetoothadapter mbluetoothadapter = Bluetoothadapter.getdefaultadapter ();if (!mbluetoothadapter.isenabled ()) {//enable () Turn Bluetooth on, this method will not pop up the prompt to turn on Bluetoothmbluetoothadapter.enable ();} }================================================android related APIs for system internal state information: get Activitymanager:activitymanager Activitymanager = (Activitymanager) this.getsystemservice (Activity_service) What is this bit of information:    Configurationinfo configurationinfo = Activitymanager.getdeviceconfigurationinfo ();   Get information about the state of the process memory: debug.memoryinfo[] Processmemoryinfo = Activitymanager.getprocessmemoryinfo (processids);    Get service information currently running:list<runningserviceinfo> Runningserviceinfos = activitymanager.getrunningservices ( MaxValue);    get the currently running task information:list<runningtaskinfo> Runningtaskinfos = Activitymanager.getrunningtasks (MaxValue); The topactivity of the Runningtaskinfos is that the active activity of the current task in the task queue returned by Getrunningtasks () is sorted according to the activity of these tasks, the more active the more forward. The first is the current active task/*** detects if a activityupdate is at the top of the current task's stack */public boolean istopactivy (String cmdName) {        Activitymanager manager = (Activitymanager) context.getsystemservice (activity_service);        List<runningtaskinfo> Runningtaskinfos = manager.getrunningtasks (1);        String cmpnametemp = null;  &nbsp ;     if (null! = Runningtaskinfos) {                cmpnametemp= (Runningtas Kinfos.get (0). topactivity). toString);                LOG.E ("Cmpname", "Cmpname: "+cmpname);       }       if (null = = Cmpnametemp) return false;    &NBSP ;  return cmpnametemp.equals (cmdName);} Finally add the required permissions in the app: <uses-permission android:name= "Android.permission.GET_TASKS"/>[java] View plaincopy /** Get the Launcher status */     Private  boolean Islauncherrunnig (context context) {   &nbs P         Boolean result = 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 (int i = 0; i < Names.size (); i++) {                               &NBS P     if (Names.get (i) equals (Running.processname)) {               &NBSP ;                             result = true;        &NBSp                          ,         &NB Sp Break                                  &NBS P  }                               & nbsp                               }  & nbsp           return result;     }  [java] View plaincopy private list<string> getallthelauncher () {   & nbsp     list<string> names = null;          Packagemanager PKGMGT = This.getpackagemanager ();          Intent it = new Intent (intent.action_main);             it.addcategory (intent.category_home);   &nBsp       list<resolveinfo> RA =pkgmgt.queryintentactivities (it,0);           if (ra.size ()! = 0) {             names = new Ar Raylist<string> ();         }          for (int i=0;i< ra.size (); i++)     &NBSP ;        {         String PackageName =  ra.get (i). activityinfo.package Name;          Names.add (PackageName);         }            return names;     }  android What ACTIVITY is currently displayed activitymanager AM = (activitymanager) getsystemservice (activity_ SERVICE); ComponentName cn = Am.getrunningtasks (1). Get (0). topactivity; LOG.D ("", "pkg:" +cn.getpackagename ()); LOG.D ("", "CLS:" +cn.getclassname ()); How does Android determine if the program is running in the foreground [Java] View plaincopy private boolean istopactivity () {      &NBsp   List<runningtaskinfo>  tasksinfo = activitymanager.getrunningtasks (1);          if (tasksinfo.size () > 0) {            //application on stack Top level              if (packagename.equals (tasksinfo.get (0). Topactivity.getpackagename ( )) {                 return true,            & nbsp }         }          return false;     }   turn from: Connect

How Android can tell if an app is running (go)

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.