您的位置:首页 > 移动开发 > Android开发

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

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



ActivityMain.java

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

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

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



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

<?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>




效果如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐