获取Android中正在运行应用程序的列表


最近想做一个任务管理器练一练手,第一道题就是获取手机中正在运行的程序。后来在网上找了一下资料,终于有了眉目。废话不多说!看代码。

ActivityMain.java

  1. public class ActivityMain extends ListActivity { 
  2.     @Override 
  3.     public void onCreate(Bundle savedInstanceState) { 
  4.         super.onCreate(savedInstanceState); 
  5.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
  6.         
  7.        List<Programe> list = getRunningProcess(); 
  8.        ListAdapter adapter = new ListAdapter(list,this); 
  9.        getListView().setAdapter(adapter); 
  10.     } 
  11.      
  12.     //正在运行的 
  13.     public List<Programe> getRunningProcess(){ 
  14.         PackagesInfo pi = new PackagesInfo(this); 
  15.          
  16.         ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
  17.         //获取正在运行的应用 
  18.         List<RunningAppProcessInfo> run = am.getRunningAppProcesses(); 
  19.         //获取包管理器,在这里主要通过包名获取程序的图标和程序名 
  20.         PackageManager pm =this.getPackageManager(); 
  21.         List<Programe> list = new ArrayList<Programe>();     
  22.          
  23.         for(RunningAppProcessInfo ra : run){ 
  24.             //这里主要是过滤系统的应用和电话应用,当然你也可以把它注释掉。 
  25.             if(ra.processName.equals("system") || ra.processName.equals("com.Android.phone")){ 
  26.                 continue
  27.             } 
  28.              
  29.             Programe  pr = new Programe(); 
  30.             pr.setIcon(pi.getInfo(ra.processName).loadIcon(pm)); 
  31.             pr.setName(pi.getInfo(ra.processName).loadLabel(pm).toString()); 
  32.             System.out.println(pi.getInfo(ra.processName).loadLabel(pm).toString()); 
  33.             list.add(pr); 
  34.         } 
  35.         return list; 
  36.     } 
  37.      
 
  1. public class ActivityMain extends ListActivity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
  6.          
  7.        List<Programe> list = getRunningProcess();  
  8.        ListAdapter adapter = new ListAdapter(list,this);  
  9.        getListView().setAdapter(adapter);  
  10.     }  
  11.       
  12.     //正在运行的   
  13.     public List<Programe> getRunningProcess(){  
  14.         PackagesInfo pi = new PackagesInfo(this);  
  15.           
  16.         ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  
  17.         //获取正在运行的应用   
  18.         List<RunningAppProcessInfo> run = am.getRunningAppProcesses();  
  19.         //获取包管理器,在这里主要通过包名获取程序的图标和程序名   
  20.         PackageManager pm =this.getPackageManager();  
  21.         List<Programe> list = new ArrayList<Programe>();      
  22.           
  23.         for(RunningAppProcessInfo ra : run){  
  24.             //这里主要是过滤系统的��用和电话应用,当然你也可以把它注释掉。   
  25.             if(ra.processName.equals("system") || ra.processName.equals("com.android.phone")){  
  26.                 continue;  
  27.             }  
  28.               
  29.             Programe  pr = new Programe();  
  30.             pr.setIcon(pi.getInfo(ra.processName).loadIcon(pm));  
  31.             pr.setName(pi.getInfo(ra.processName).loadLabel(pm).toString());  
  32.             System.out.println(pi.getInfo(ra.processName).loadLabel(pm).toString());  
  33.             list.add(pr);  
  34.         }  
  35.         return list;  
  36.     }  
  37.       
  38. }  

ListAdapter.java

  1. public class ListAdapter extends BaseAdapter { 
  2.     List<Programe> list = new ArrayList<Programe>(); 
  3.     LayoutInflater la; 
  4.     Context context; 
  5.      
  6.     public ListAdapter(List<Programe> list ,Context context){ 
  7.         this.list = list; 
  8.         this.context = context; 
  9.     } 
  10.      
  11.     @Override 
  12.     public int getCount() { 
  13.         // TODO Auto-generated method stub 
  14.         return list.size(); 
  15.     } 
  16.     @Override 
  17.     public Object getItem(int position) { 
  18.         // TODO Auto-generated method stub 
  19.         return list.get(position); 
  20.     } 
  21.     @Override 
  22.     public long getItemId(int position) { 
  23.         // TODO Auto-generated method stub 
  24.         return position; 
  25.     } 
  26.     @Override 
  27.     public View getView(int position, View convertView, ViewGroup parent) { 
  28.         ViewHolder holder; 
  29.         if(convertView == null
  30.         {   
  31.             la = LayoutInflater.from(context); 
  32.             convertView=la.inflate(R.layout.list_item, null); 
  33.              
  34.             holder = new ViewHolder(); 
  35.             holder.imgage=(ImageView) convertView.findViewById(R.id.image); 
  36.             holder.text = (TextView) convertView.findViewById(R.id.text); 
  37.              
  38.             convertView.setTag(holder); 
  39.         }else
  40.             holder = (ViewHolder) convertView.getTag(); 
  41.         } 
  42.          final Programe pr = (Programe)list.get(position); 
  43.         //设置图标 
  44.         holder.imgage.setImageDrawable(pr.getIcon()); 
  45.         //设置程序名 
  46.         holder.text.setText(pr.getName()); 
  47.          
  48.         return convertView; 
  49.     } 
  50. class ViewHolder{ 
  51.      TextView text; 
  52.     ImageView imgage; 
 
  1. public class ListAdapter extends BaseAdapter {  
  2.     List<Programe> list = new ArrayList<Programe>();  
  3.     LayoutInflater la;  
  4.     Context context;  
  5.       
  6.     public ListAdapter(List<Programe> list ,Context context){  
  7.         this.list = list;  
  8.         this.context = context;  
  9.     }  
  10.       
  11.     @Override  
  12.     public int getCount() {  
  13.         // TODO Auto-generated method stub   
  14.         return list.size();  
  15.     }  
  16.     @Override  
  17.     public Object getItem(int position) {  
  18.         // TODO Auto-generated method stub   
  19.         return list.get(position);  
  20.     }  
  21.     @Override  
  22.     public long getItemId(int position) {  
  23.         // TODO Auto-generated method stub   
  24.         return position;  
  25.     }  
  26.     @Override  
  27.     public View getView(int position, View convertView, ViewGroup parent) {  
  28.         ViewHolder holder;  
  29.         if(convertView == null)  
  30.         {    
  31.             la = LayoutInflater.from(context);  
  32.             convertView=la.inflate(R.layout.list_item, null);  
  33.               
  34.             holder = new ViewHolder();  
  35.             holder.imgage=(ImageView) convertView.findViewById(R.id.image);  
  36.             holder.text = (TextView) convertView.findViewById(R.id.text);  
  37.               
  38.             convertView.setTag(holder);  
  39.         }else{  
  40.             holder = (ViewHolder) convertView.getTag();  
  41.         }  
  42.          final Programe pr = (Programe)list.get(position);  
  43.         //设置图标   
  44.         holder.imgage.setImageDrawable(pr.getIcon());  
  45.         //设置程序名   
  46.         holder.text.setText(pr.getName());  
  47.           
  48.         return convertView;  
  49.     }  
  50. }  
  51. class ViewHolder{  
  52.      TextView text;  
  53.     ImageView imgage;  
  54. }  

PackagesInfo.java

  1. public class PackagesInfo { 
  2.     private List<ApplicationInfo> appList; 
  3.      
  4.     public PackagesInfo(Context context){ 
  5.         //通包管理器,检索所有的应用程序(甚至卸载的)与数据目录 
  6.         PackageManager pm = context.getApplicationContext().getPackageManager(); 
  7.         appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES); 
  8.     } 
  9.      
  10.      
  11.      
  12.     /**
  13.      * 通过一个程序名返回该程序的一个Application对象。
  14.      * @param name  程序名
  15.      * @return  ApplicationInfo
  16.      */ 
  17.      
  18.     public ApplicationInfo getInfo(String name){ 
  19.         if(name == null){ 
  20.             return null
  21.         } 
  22.         for(ApplicationInfo appinfo : appList){ 
  23.             if(name.equals(appinfo.processName)){ 
  24.                 return appinfo; 
  25.             } 
  26.         } 
  27.         return null
  28.     } 
  29.      
 
  1. public class PackagesInfo {  
  2.     private List<ApplicationInfo> appList;  
  3.       
  4.     public PackagesInfo(Context context){  
  5.         //通包管理器,检索所有的应用程序(甚至卸载的)与数据目录   
  6.         PackageManager pm = context.getApplicationContext().getPackageManager();  
  7.         appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);  
  8.     }  
  9.       
  10.       
  11.       
  12.     /** 
  13.      * 通过一个程序名返回该程序的一个Application对象。 
  14.      * @param name  程序名 
  15.      * @return  ApplicationInfo  
  16.      */  
  17.       
  18.     public ApplicationInfo getInfo(String name){  
  19.         if(name == null){  
  20.             return null;  
  21.         }  
  22.         for(ApplicationInfo appinfo : appList){  
  23.             if(name.equals(appinfo.processName)){  
  24.                 return appinfo;  
  25.             }  
  26.         }  
  27.         return null;  
  28.     }  
  29.       
  30. }  

Programe.java

  1. public class Programe { 
  2.     //图标 
  3.     private Drawable icon;   
  4.     //程序名 
  5.     private String name; 
  6.      
  7.     public Drawable getIcon() { 
  8.         return icon; 
  9.     } 
  10.     public void setIcon(Drawable icon) { 
  11.         this.icon = icon; 
  12.     } 
  13.     public String getName() { 
  14.         return name; 
  15.     } 
  16.     public void setName(String name) { 
  17.         this.name = name; 
  18.     } 
  19.      
 
  1. public class Programe {  
  2.     //图标   
  3.     private Drawable icon;    
  4.     //程序名   
  5.     private String name;  
  6.       
  7.     public Drawable getIcon() {  
  8.         return icon;  
  9.     }  
  10.     public void setIcon(Drawable icon) {  
  11.         this.icon = icon;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.       
  20. }  

list_item.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="horizontal" 
  4.     android:layout_width="wrap_content" 
  5.     android:layout_height="wrap_content" 
  6.    > 
  7.     <ImageView 
  8.         android:id="@+id/image" 
  9.         android:layout_width="wrap_content" 
  10.         android:layout_height="wrap_content" 
  11.         android:layout_marginRight="10dip" 
  12.     />  
  13.   <TextView 
  14.     android:id="@+id/text" 
  15.     android:layout_width="wrap_content" 
  16.     android:layout_height="wrap_content" 
  17.   /> 
  18.     
  19.    </LinearLayout> 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.    >  
  7.     <ImageView  
  8.         android:id="@+id/image"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginRight="10dip"  
  12.     />   
  13.   <TextView  
  14.     android:id="@+id/text"  
  15.     android:layout_width="wrap_content"  
  16.     android:layout_height="wrap_content"  
  17.   />  
  18.      
  19.    </LinearLayout>  

效果如图:

相关内容