获取本机安装的应用及启动

效果图:




说明:

第一行:应用程序名称

第二行:应用程序包名

第三行:应用程序入口Activity名称


代码如下:


  1. package com.hello.project;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import android.app.Activity;  
  11. import android.app.ProgressDialog;  
  12. import android.content.ComponentName;  
  13. import android.content.Context;  
  14. import android.content.Intent;  
  15. import android.content.pm.ApplicationInfo;  
  16. import android.content.pm.PackageManager;  
  17. import android.content.pm.ResolveInfo;  
  18. import android.graphics.drawable.Drawable;  
  19. import android.net.Uri;  
  20. import android.os.Build;  
  21. import android.os.Bundle;  
  22. import android.os.Handler;  
  23. import android.os.Message;  
  24. import android.provider.Settings;  
  25. import android.util.Log;  
  26. import android.view.LayoutInflater;  
  27. import android.view.View;  
  28. import android.view.ViewGroup;  
  29. import android.widget.AbsListView;  
  30. import android.widget.AdapterView;  
  31. import android.widget.AbsListView.OnScrollListener;  
  32. import android.widget.AdapterView.OnItemClickListener;  
  33. import android.widget.BaseAdapter;  
  34. import android.widget.ImageView;  
  35. import android.widget.ListView;  
  36. import android.widget.SimpleAdapter;  
  37. import android.widget.TextView;  
  38.   
  39. public class SoftActivity extends Activity implements Runnable ,OnItemClickListener{  
  40.       
  41.     private List<Map<String, Object>> list = null;  
  42.     private ListView softlist = null;  
  43.     private ProgressDialog pd;  
  44.     private Context mContext;  
  45.     private PackageManager mPackageManager;  
  46.     private List<ResolveInfo> mAllApps;  
  47.   
  48.     @Override  
  49.     protected void onCreate(Bundle savedInstanceState) {  
  50.         setContentView(R.layout.software);  
  51.         setTitle("文件管理器");  
  52.           
  53.         mContext = this;  
  54.         mPackageManager = getPackageManager();  
  55.           
  56.         softlist = (ListView) findViewById(R.id.softlist);  
  57.   
  58.          pd = ProgressDialog.show(this"请稍候..""正在收集软件信息..."true,false);  
  59.          Thread thread = new Thread(this);  
  60.          thread.start();  
  61.   
  62.         super.onCreate(savedInstanceState);  
  63.     }  
  64.     /** 
  65.      * 检查系统应用程序,添加到应用列表中 
  66.      */  
  67.     private void bindMsg(){  
  68.         //应用过滤条件  
  69.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  70.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  71.         mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);  
  72.         softlist.setAdapter(new MyAdapter(mContext, mAllApps));  
  73.         softlist.setOnItemClickListener(this);  
  74.         //按报名排序  
  75.         Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));  
  76.           
  77.     }  
  78.       
  79.     @Override  
  80.     public void run() {  
  81.         bindMsg();  
  82.         handler.sendEmptyMessage(0);  
  83.   
  84.     }  
  85.     private Handler handler = new Handler() {  
  86.         public void handleMessage(Message msg) {  
  87.             pd.dismiss();  
  88.         }  
  89.     };  
  90.       
  91.     class MyAdapter extends BaseAdapter{  
  92.   
  93.         private Context context;  
  94.         private List<ResolveInfo> resInfo;  
  95.         private ResolveInfo res;  
  96.         private LayoutInflater infater=null;     
  97.           
  98.         public MyAdapter(Context context, List<ResolveInfo> resInfo) {              
  99.             this.context = context;  
  100.             this.resInfo = resInfo;  
  101.              infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
  102.         }  
  103.   
  104.         @Override  
  105.         public int getCount() {  
  106.               
  107.             return resInfo.size();  
  108.         }  
  109.   
  110.         @Override  
  111.         public Object getItem(int arg0) {  
  112.               
  113.             return arg0;  
  114.         }  
  115.   
  116.         @Override  
  117.         public long getItemId(int position) {  
  118.               
  119.             return position;  
  120.         }  
  121.   
  122.         @Override  
  123.         public View getView(int position, View convertView, ViewGroup parent) {  
  124.               
  125.         //  View view = null;    
  126.             ViewHolder holder = null;    
  127.             if (convertView == null || convertView.getTag() == null) {    
  128.                 convertView = infater.inflate(R.layout.soft_row, null);    
  129.                 holder = new ViewHolder(convertView);    
  130.                 convertView.setTag(holder);    
  131.             }     
  132.             else{    
  133.            //     view = convertView ;    
  134.                 holder = (ViewHolder) convertView.getTag() ;    
  135.             }    
  136.             //获取应用程序包名,程序名称,程序图标  
  137.             res = resInfo.get(position);  
  138.             holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager));    
  139.             holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());  
  140.             holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);  
  141.             return convertView;  
  142.               
  143.             /*convertView = LayoutInflater.from(context).inflate(R.layout.soft_row, null); 
  144.                      
  145.             app_icon = (ImageView)convertView.findViewById(R.id.img); 
  146.             app_tilte = (TextView)convertView.findViewById(R.id.name); 
  147.             app_des = (TextView)convertView.findViewById(R.id.desc); 
  148.  
  149.             res = resInfo.get(position); 
  150.             app_icon.setImageDrawable(res.loadIcon(mPackageManager)); 
  151.             app_tilte.setText(res.loadLabel(mPackageManager).toString()); 
  152.             app_des.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name); 
  153.  
  154.             return convertView;*/  
  155.         }  
  156.     }  
  157.     //设定界面布局  
  158.     class ViewHolder {    
  159.         ImageView appIcon;    
  160.         TextView tvAppLabel;    
  161.         TextView tvPkgName;    
  162.     
  163.         public ViewHolder(View view) {    
  164.             this.appIcon = (ImageView) view.findViewById(R.id.img);    
  165.             this.tvAppLabel = (TextView) view.findViewById(R.id.name);    
  166.             this.tvPkgName = (TextView) view.findViewById(R.id.desc);    
  167.         }    
  168.     }    
  169.     /** 
  170.      * 单击应用程序后进入系统应用管理界面 
  171.      */  
  172.     @Override  
  173.     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {  
  174.           
  175.         ResolveInfo res = mAllApps.get(position);  
  176.         //该应用的包名和主Activity  
  177.         String pkg = res.activityInfo.packageName;  
  178.         String cls = res.activityInfo.name;  
  179.           
  180.         ComponentName componet = new ComponentName(pkg, cls);  
  181.           
  182.         Intent i = new Intent();  
  183.         i.setComponent(componet);  
  184.         startActivity(i);  
  185.           
  186.     }  
  187.   
  188. }  
package com.hello.project;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SoftActivity extends Activity implements Runnable ,OnItemClickListener{
	
	private List<Map<String, Object>> list = null;
	private ListView softlist = null;
	private ProgressDialog pd;
	private Context mContext;
	private PackageManager mPackageManager;
	private List<ResolveInfo> mAllApps;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.software);
		setTitle("文件管理器");
		
		mContext = this;
    	mPackageManager = getPackageManager();
    	
		softlist = (ListView) findViewById(R.id.softlist);

		 pd = ProgressDialog.show(this, "请稍候..", "正在收集软件信息...", true,false);
		 Thread thread = new Thread(this);
	     thread.start();

		super.onCreate(savedInstanceState);
	}
	/**
	 * 检查系统应用程序,添加到应用列表中
	 */
	private void bindMsg(){
		//应用过滤条件
    	Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    	mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
    	softlist.setAdapter(new MyAdapter(mContext, mAllApps));
    	softlist.setOnItemClickListener(this);
    	//按报名排序
        Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
        
	}
	
	@Override
	public void run() {
		bindMsg();
	    handler.sendEmptyMessage(0);

	}
	private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            pd.dismiss();
        }
    };
    
    class MyAdapter extends BaseAdapter{

		private Context context;
    	private List<ResolveInfo> resInfo;
    	private ResolveInfo res;
		private LayoutInflater infater=null;   
    	
    	public MyAdapter(Context context, List<ResolveInfo> resInfo) {			
			this.context = context;
			this.resInfo = resInfo;
			 infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		}

		@Override
		public int getCount() {
			
			return resInfo.size();
		}

		@Override
		public Object getItem(int arg0) {
			
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
		//	View view = null;  
	        ViewHolder holder = null;  
	        if (convertView == null || convertView.getTag() == null) {  
	        	convertView = infater.inflate(R.layout.soft_row, null);  
	            holder = new ViewHolder(convertView);  
	            convertView.setTag(holder);  
	        }   
	        else{  
	       //     view = convertView ;  
	            holder = (ViewHolder) convertView.getTag() ;  
	        }  
	        //获取应用程序包名,程序名称,程序图标
	        res = resInfo.get(position);
	        holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager));  
	        holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());
	        holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
			return convertView;
			
			/*convertView = LayoutInflater.from(context).inflate(R.layout.soft_row, null);
					
			app_icon = (ImageView)convertView.findViewById(R.id.img);
			app_tilte = (TextView)convertView.findViewById(R.id.name);
			app_des = (TextView)convertView.findViewById(R.id.desc);

			res = resInfo.get(position);
			app_icon.setImageDrawable(res.loadIcon(mPackageManager));
			app_tilte.setText(res.loadLabel(mPackageManager).toString());
			app_des.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);

			return convertView;*/
		}
    }
    //设定界面布局
    class ViewHolder {  
        ImageView appIcon;  
        TextView tvAppLabel;  
        TextView tvPkgName;  
  
        public ViewHolder(View view) {  
            this.appIcon = (ImageView) view.findViewById(R.id.img);  
            this.tvAppLabel = (TextView) view.findViewById(R.id.name);  
            this.tvPkgName = (TextView) view.findViewById(R.id.desc);  
        }  
    }  
    /**
     * 单击应用程序后进入系统应用管理界面
     */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		
		ResolveInfo res = mAllApps.get(position);
		//该应用的包名和主Activity
		String pkg = res.activityInfo.packageName;
		String cls = res.activityInfo.name;
		
		ComponentName componet = new ComponentName(pkg, cls);
		
		Intent i = new Intent();
		i.setComponent(componet);
		startActivity(i);
		
	}

}

software.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <ListView   
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"   
  9.         android:drawSelectorOnTop="false"  
  10.         android:id="@+id/softlist" />  
  11.           
  12. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<ListView 
	    android:layout_width="fill_parent"
		android:layout_height="fill_parent" 
		android:drawSelectorOnTop="false"
		android:id="@+id/softlist" />
		
</LinearLayout>

soft_row.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/vw1"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:orientation="horizontal">      
  8.   
  9.     <ImageView android:id="@+id/img"  
  10.         android:layout_width="32dip"  
  11.         android:layout_margin="4dip"  
  12.         android:layout_height="32dip"/>  
  13.    
  14.    <LinearLayout  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:orientation="vertical">  
  18.   
  19.         <TextView android:id="@+id/name"  
  20.             android:textSize="18sp"  
  21.             android:textStyle="bold"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"/>  
  24.   
  25.         <TextView android:id="@+id/desc"  
  26.             android:textSize="14sp"  
  27.             android:layout_width="fill_parent"  
  28.             android:paddingLeft="10dip"  
  29.             android:layout_height="wrap_content"/>  
  30.   
  31.     </LinearLayout>  
  32.   
  33. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">    

    <ImageView android:id="@+id/img"
        android:layout_width="32dip"
        android:layout_margin="4dip"
        android:layout_height="32dip"/>
 
   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/name"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <TextView android:id="@+id/desc"
            android:textSize="14sp"
            android:layout_width="fill_parent"
            android:paddingLeft="10dip"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

如有疑问请留言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值