Obtain the list of applications running in android

Source: Internet
Author: User

Recently, I want to practice a task manager. The first question is to get a program running on the mobile phone. Later, I found some information on the Internet and finally got an eye on it. Not much nonsense! Check the code.

 

ActivityMain. java

Public class ActivityMain extends ListActivity {<br/> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> requestWindowFeature (Window. FEATURE_INDETERMINATE_PROGRESS); </p> <p> List <Programe> list = getRunningProcess (); <br/> ListAdapter adapter = new ListAdapter (list, this ); <br/> getListView (). setAdapter (adapter); <br/>}</p> <p> // running <br /> Public List <Programe> getRunningProcess () {<br/> PackagesInfo pi = new PackagesInfo (this); </p> <p> ActivityManager am = (ActivityManager) getSystemService (ACTIVITY_SERVICE); <br/> // obtain the running application <br/> List <RunningAppProcessInfo> run = am. getRunningAppProcesses (); <br/> // obtain the Package Manager. The package name is used to obtain the program icon and program name. <br/> PackageManager pm = this. getPackageManager (); <br/> List <Programe> list = new ArrayList <Programe> (); </p> <P> for (RunningAppProcessInfo ra: run) {<br/> // This is mainly used to filter system applications and telephone applications. Of course, you can also comment out it. <Br/> if (ra. processName. equals ("system") | ra. processName. equals ("com. android. phone ") {<br/> continue; <br/>}</p> <p> Programe pr = new Programe (); <br/> pr. setIcon (pi. getInfo (ra. processName ). loadIcon (pm); <br/> pr. setName (pi. getInfo (ra. processName ). loadLabel (pm ). toString (); <br/> System. out. println (pi. getInfo (ra. processName ). loadLabel (pm ). toString (); <br/> list. add (pr); <br/>}< br/> return list; <br/>}</p> <p>} 

 

ListAdapter. java

Public class ListAdapter extends BaseAdapter {<br/> List <Programe> list = new ArrayList <Programe> (); <br/> LayoutInflater la; <br/> Context context; </p> <p> public ListAdapter (List <Programe> list, Context context) {<br/> this. list = list; <br/> this. context = context; <br/>}</p> <p> @ Override <br/> public int getCount () {<br/> // TODO Auto-generated method stub <br/> return list. size (); <br/>}< br/> @ Override <br/> public Object getItem (int position) {<br/> // TODO Auto-generated method stub <br/> return list. get (position); <br/>}< br/> @ Override <br/> public long getItemId (int position) {<br/> // TODO Auto-generated method stub <br/> return position; <br/>}< br/> @ Override <br/> public View getView (int position, View convertView, ViewGroup parent) {<br/> ViewHolder holder; <br/> if (convertView = null) <br/>{< br/> la = LayoutInflater. from (context); <br/> convertView = la. inflate (R. layout. list_item, null); </p> <p> holder = new ViewHolder (); <br/> holder. imgage = (ImageView) convertView. findViewById (R. id. image); <br/> holder. text = (TextView) convertView. findViewById (R. id. text); </p> <p> convertView. setTag (holder); <br/>}else {<br/> holder = (ViewHolder) convertView. getTag (); <br/>}< br/> final Programe pr = (Programe) list. get (position); <br/> // set the icon <br/> holder. imgage. setImageDrawable (pr. getIcon (); <br/> // set the program name <br/> holder. text. setText (pr. getName (); </p> <p> return convertView; <br/>}< br/> class ViewHolder {<br/> TextView text; <br/> ImageView imgage; <br/>} 

 

PackagesInfo. java

Public class PackagesInfo {<br/> private List <ApplicationInfo> appList; </p> <p> public PackagesInfo (Context context) {<br/> // Package Manager, retrieve all applications (or even uninstalled) and Data Directories <br/> PackageManager pm = context. getApplicationContext (). getPackageManager (); <br/> appList = pm. getInstalledApplications (PackageManager. GET_UNINSTALLED_PACKAGES); <br/>}</p> <p>/** <br/> * returns an Application Object of the program through a program name. <Br/> * @ param name <br/> * @ returnApplicationInfo <br/> */</p> <p> public ApplicationInfo getInfo (String name) {<br/> if (name = null) {<br/> return null; <br/>}< br/> for (ApplicationInfo appinfo: appList) {<br/> if (name. equals (appinfo. processName) {<br/> return appinfo; <br/>}< br/> return null; <br/>}</p> <p >}< br/> 

Programe. java

 

Public class Programe {<br/> // icon <br/> private Drawable icon; <br/> // program name <br/> private String name; </p> <p> public Drawable getIcon () {<br/> return icon; <br/>}< br/> public void setIcon (Drawable icon) {<br/> this. icon = icon; <br/>}< br/> public String getName () {<br/> return name; <br/>}< br/> public void setName (String name) {<br/> this. name = name; <br/>}</p> <p>} 

 

List_item.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "horizontal" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> <ImageView <br/> android: id = "@ + id/image" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: layout_marginRight = "10dip" <br/> <TextView <br/> android: id = "@ + id/text" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> </p> </LinearLayout> 

 

Effect

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.