Obtain the running application in Android-activitymanager. runningappprocessinfo class

Source: Internet
Author: User

 

Reprinted please indicate the source:Http://blog.csdn.net/qinjuning

 

Today, I will continue to explain how to use activitymanager. Through the previous sections, we learned how to use activitymanager to obtain

A running process. The knowledge point of this article is to use the process information to obtain the running applications in the system and the applications running in each process.

The knowledge points used include the use of packagemanager 《Use of packagemanager"

Every application runs in its own process, but to save resources or to complete a common job, they

It may also run in a process.

 

 

Knowledge Point introduction:

Activitymanager. runningappprocessinfo class description: encapsulates information about running processes.

Common fields:

Int PID process ID

ID of the int uid Process

String processname process name. The default value is package name or specified by Android: Process = ".

String []PkglistAll application package names running in this process

 


For more information about activitymanager. runningappprocessinfo, see <Application Information in Android --- use of activitymanager>

Packagemanger class

Note: operations on application information are encapsulated.

The method for obtaining application information is as follows:

Public abstract applicationinfoGetapplicationinfo(String packagename,
Int flags)

Parameter: packagename package name

Flags the applicationinfo is the flags mark, which can usually be directly assigned a constant0You can.

Function: returns the applicationinfo object.

 

For more information about packagemanger, see <obtain application (Package) Information in Android ----- use of packagemanager (1)>

 

 

Demo description:

 

We use activitymanager to obtain information about all running processes, that is, get the name of the running application package (pkgname) in each process ),

Then, through these Package Names (pkgname), you can directly call the method provided by the packagemanager class to obtain information about these applications.

 

Some resource files are not pasted, and the main project logic is directly pasted.Note:Here we get all the application information at a time, and then

The application filters out the expected objects. Readers can use the method provided by the packagemanager class to traverse all Package Names (pkgname) cyclically ),

However, the efficiency will be relatively low.

 

As follows:

Click a process to view information about the application running a process:

The project code that displays the running application is as follows:

 

Package COM. qin. AMMP; import Java. util. arraylist; import Java. util. collections; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import android. app. activitymanager; import android. content. context; import android. content. intent; import android. content. PM. applicationinfo; import android. content. PM. packageinfo; import android. content. PM. packagemanager; import android Oid. content. PM. packagemanager. namenotfoundexception; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. adapterview; import android. widget. listview; import android. widget. textview; import android. widget. adapterview. onitemclicklistener; public class browserunningappactivity extends activity {Private Static string tag = "browserunningappactivity"; private listv Iew listview = NULL; private list <runningappinfo> mlistappinfo = NULL; private textview tvinfo = NULL; private packagemanager PM; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. browse_app_list); listview = (listview) findviewbyid (R. id. listviewapp); tvinfo = (textview) findviewbyid (R. id. tvinfo); mlistappinfo = new arraylist <runningap Pinfo> (); // query all application intent = getintent () of a specific process; // whether to query application int pid = intent of a specific PID. getintextra ("extra_process_id",-1); If (PID! =-1) {// all running applications in a specific frequency, mlistappinfo = queryspecailpidrunningappinfo (intent, pid );} else {// query information about all running applications, including the process ID and process name tvinfo. settext ("all running applications include -------"); mlistappinfo = queryallrunningappinfo ();} browserunningappadapter browseappadapter = new browserunningappadapter (this, mlistappinfo); listview. setadapter (browseappadapter);} // query information about all running applications: including the process ID and process name where they are located // here I directly obtain all the applications installed in the system, then obtain the private list <runningappinfo> queryallrunningappinfo () {PM = This. getpackagemanager (); // query the list of all installed applications <applicationinfo> listappcations = PM. getinstalledapplications (packagemanager. get_uninstalled_packages); collections. sort (listappcations, new applicationinfo. displaynamecomparator (PM); // sort // Save the names of all running packages and the information of their processes. Map <string, activitymanager. runningappprocessinfo> pgkprocessappmap = new hashmap <string, activitymanager. runningappprocessinfo> (); activitymanager mactivitymanager = (activitymanager) getsystemservice (context. activity_service); // call the getrunningappprocesses () method of activitymanager to obtain the list of all running processes in the System <activitymanager. runningappprocessinfo> appprocesslist = mactivitymanager. getrunningappprocesses (); For (activitymanager. runningappprocessinfo appprocess: appprocesslist) {int pid = appprocess. PID; // pidstring processname = appprocess. processname; // process name log. I (TAG, "processname:" + processname + "PID:" + PID); string [] pkgnamelist = appprocess. pkglist; // obtain all application packages running in the process // The package name of all output applications for (INT I = 0; I <pkgnamelist. length; I ++) {string pkgname = pkgnamelist [I]; log. I (TAG, "packagename" + pkgname + "at Index" + I + "in process" + PID); // Add it to pgkprocessappmap in the map object. put (pkgname, appprocess) ;}// Save the list of all running applications <runningappinfo> runningappinfos = new arraylist <runningappinfo> (); // Save the filtered appinfofor (applicationinfo app: listappcations) {// if the package name exists, construct a runningappinfo object if (pgkprocessappmap. containskey (App. packagename) {// obtain the PID and processnameint pid of the packagename = pgkprocessappmap. get (App. packagename ). PID; string processname = pgkprocessappmap. get (App. packagename ). processname; runningappinfos. add (getappinfo (app, PID, processname) ;}return runningappinfos ;} // Private list <runningappinfo> queryspecailpidrunningappinfo (intent, int PID) of all running applications in a particular frequency {string [] pkgnamelist = intent. getstringarrayextra ("extra_pkgnamelist"); string processname = intent. getstringextra ("extra_process_name"); // update uitvinfo. settext ("process ID:" + PID + "running applications:" + pkgnamelist. length); PM = This. getpackagemanager (); // Save the list of all running applications <runningappinfo> runningappinfos = new arraylist <runningappinfo> (); // Save the appinfofor (INT I = 0; I <pkgnamelist. length; I ++) {// query the specific applicationinfo object applicationinfo appinfo Based on the package name; try {appinfo = PM. getapplicationinfo (pkgnamelist [I], 0); runningappinfos. add (getappinfo (appinfo, PID, processname);} catch (namenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace () ;}// 0 indicates that there is no mark;} return runningappinfos;} // construct a runningappinfo object and assign the value private runningappinfo getappinfo (applicationinfo app, int PID, string processname) {runningappinfo appinfo = new runningappinfo (); appinfo. setapplabel (string) app. loadlabel (PM); appinfo. setappicon (App. loadicon (PM); appinfo. setpkgname (App. packagename); appinfo. setpid (PID); appinfo. setprocessname (processname); Return appinfo ;}}

Through the introduction of this section, we have basically completed the functions of the application in the settings module of the Android system, although it is not integrated as a whole,

However, you have already planned the main axis.

 

 

Code: http://download.csdn.net/detail/qinjuning/3839045

 

 

 

 

 

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.