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

转自:http://blog.csdn.net/ghd2000/article/details/6128500

 

ActivityMain.java

view plain copy to clipboard print ?
  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.      

public class ActivityMain extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); List<Programe> list = getRunningProcess(); ListAdapter adapter = new ListAdapter(list,this); getListView().setAdapter(adapter); } //正在运行的 public List<Programe> getRunningProcess(){ PackagesInfo pi = new PackagesInfo(this); ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); //获取正在运行的应用 List<RunningAppProcessInfo> run = am.getRunningAppProcesses(); //获取包管理器,在这里主要通过包名获取程序的图标和程序名 PackageManager pm =this.getPackageManager(); List<Programe> list = new ArrayList<Programe>(); for(RunningAppProcessInfo ra : run){ //这里主要是过滤系统的应用和电话应用,当然你也可以把它注释掉。 if(ra.processName.equals("system") || ra.processName.equals("com.android.phone")){ continue; } Programe pr = new Programe(); pr.setIcon(pi.getInfo(ra.processName).loadIcon(pm)); pr.setName(pi.getInfo(ra.processName).loadLabel(pm).toString()); System.out.println(pi.getInfo(ra.processName).loadLabel(pm).toString()); list.add(pr); } return list; } }

ListAdapter.java

view plain copy to clipboard print ?
  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; 

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

PackagesInfo.java

view plain copy to clipboard print ?
  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.      

public class PackagesInfo { private List<ApplicationInfo> appList; public PackagesInfo(Context context){ //通包管理器,检索所有的应用程序(甚至卸载的)与数据目录 PackageManager pm = context.getApplicationContext().getPackageManager(); appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES); } /** * 通过一个程序名返回该程序的一个Application对象。 * @param name 程序名 * @return ApplicationInfo */ public ApplicationInfo getInfo(String name){ if(name == null){ return null; } for(ApplicationInfo appinfo : appList){ if(name.equals(appinfo.processName)){ return appinfo; } } return null; } }

Programe.java

view plain copy to clipboard print ?
  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.      

public class Programe { //图标 private Drawable icon; //程序名 private String name; public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

list_item.xml

view plain copy to clipboard print ?
  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> 

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

效果如图:

获取android中正在运行应用程序的列表_第1张图片

 

你可能感兴趣的:(android,String,list,layout,null,Class)