How does Android determine whether an application is running?

Source: Internet
Author: User
How does Android determine whether an application is running in an application, or whether an application is running in a service or consumer, so as to perform some related processing. In this case, we need to get an activitymanager, which is used to manage the activity. It has a method called getrunningtasks to obtain the list of tasks currently running in the system. The Code is as follows: activitymanager AM = (activitymanager) context. getsystemservice (context. activity_service); List <runningtaskinfo> List = aM. getrunningtasks (100); For (runningtaskinfo info: List) {If (info. topactivity. getpackagename (). equals (my_pkg_name) & info. baseactivity. getpackagename (). equals (my_pkg_name )) {Isapprunning = true; // find it, break;} 100 indicates the maximum number of tasks, info. topactivity indicates the currently running activity, info. this process is running in the background of the baseactivity table system. The specific judgment depends on your own business needs. This class also provides more methods to obtain the system running services, memory usage, and other methods. Please search for them by yourself. Note that if you want to run this method properly, please go to your androidmanifest. add: <uses-Permission Android: Name = "android. permission. get_tasks "/> otherwise, an exception may be thrown. ======================================================================/// Judge whether the application is running activitymanager AM = (activitymanager) context. getsystemservice (context. activity_service); List <runningtaskinfo> List = aM. getrunningtasks (100); 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;} // restart the Bluetooth only when the application is running. Otherwise, Bluetooth cannot be disabled after the application is installed. If (isapprunning) {descrithadapter mbluw.thadapter = descrithadapter. getdefaadapter adapter (); If (! Mblustmthadapter. isenabled () {// enable () to enable Bluetooth. This method does not display the message mblustmthadapter. enable ();}} ========================================================== ======== APIs related to the internal status information of the Android system: activitymanager: activitymanager = (activitymanager) This. getsystemservice (activity_service): configurationinfo = activitymanager. getdeviceconfigurationinfo (); get the process memory status information: Debug. memoryinfo [] Pro Cessmemoryinfo = activitymanager. getprocessmemoryinfo (processids); get the currently running service information: List <runningserviceinfo> runningserviceinfos = activitymanager. getrunningservices (maxvalue); obtain information about the currently running task: List <runningtaskinfo> runningtaskinfos = activitymanager. getrunningtasks (maxvalue); among them, the topactivity of runningtaskinfos is the active activity of the current task. In the task queue returned by getrunningtasks (), the system will sort the activity of these tasks. The first is whether the current active task/*** checks whether an activityupdate is at the top of the current task stack */Public Boolean istopactivy (string rule name) {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; return cmpnametemp. equals (permission name);} finally add the required permissions to the application: <uses-Permission Android: Name = "android. permission. get_tasks "/> [Java] view plaincopy/** get the launcher status */private Boolean islauncherrunnig (context) {boolean result = false; Li St <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 ++) {If (names. get (I ). equals (running. processname) {result = true; break ;}}} return result;} [Java] view plaincopy private list <string> getallthelauncher () {list <string> names = NULL; packagemanager pkgmgt = This. 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 obtains the activitymanager AM = (activitymanager) getsystemservice (activity_service) of the currently displayed activity, and componentname Cn = aM. getrunningtasks (1 ). get (0 ). topactivity; log. D ("", "PKG:" + CN. getpackagename (); log. D ("", "CLS:" + CN. getclassname (); how does Android determine whether a program runs [Java] view plaincopy private Boolean istopactivity () {list <runningtaskinfo> tasksinfo = activitymanager. getrunningtasks (1); If (tasksinfo. size ()> 0) {// The application is located at the top layer of the stack if (packagename. equals (tasksinfo. get (0 ). topactivity. getpackagename () {return true ;}} return false ;}
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.