得到手机上已经安装过的程序

Android程序中,难免会遇到一些在当前一个程序中获取到手机中的其他程序可以进行点击运行的效果:如下Demo实现获取手机已经安装过后的APP的图标,相当于系统中的程序管理:

首先进行我们需要布局,在Activity 中放一个listView,然后建一个item.xml,里面可以只存放一个ImageView 和TextView,根据自己需要而定:
数据封装类,我们要获取APP的图标就需要存在一个集合中:

    private String appLabel;    //应用程序标签  
    private Drawable appIcon ;  //应用程序图像  
    private Intent intent ;     //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity  
    private String pkgName ;    //应用程序所对应的包名  
对这四个类型进行封装;
在Activity中:
<pre name="code" class="html">public class MainActivity extends Activity{
	private ListView listview; // listView
	private List<AppData> list; // 得到APP的集合
	private ApplicationInfoAdapter adapter;// 适配器

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 实例化
		listview = (ListView) findViewById(R.id.listviewApp);
		list = new ArrayList<AppData>();
		queryAppInfo(); // 查询所有应用程序信息
		adapter = new ApplicationInfoAdapter(this, list);//实例化
		listview.setAdapter(adapter);//listView数据填充
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.i("listView", "点击相应");
				Intent intent = list.get(position).getIntent();
				startActivity(intent);
			}
		});
	}

	// 获得所有启动Activity的信息,类似于Launch界面
	public void queryAppInfo() {
		PackageManager pm = this.getPackageManager(); // 获得PackageManager对象
		Intent intent = new Intent(Intent.ACTION_MAIN, null);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		// 通过查询,获得所有ResolveInfo对象.
		List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent,
				PackageManager.GET_INTENT_FILTERS);//这个地方可以根据情况而定,这里是加载所有已经安装存在的程序
		// 调用系统排序 , 根据name排序
		// 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序
		
		Collections.sort(resolveInfos,
				new ResolveInfo.DisplayNameComparator(pm));
		if (list != null) {
			list.clear();
			for (ResolveInfo reInfo : resolveInfos) {
				String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
				String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
				String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
				Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
				// 为应用程序的启动Activity 准备Intent
				Intent launchIntent = new Intent();
				launchIntent.setComponent(new ComponentName(pkgName,
						activityName));
				// 创建一个AppInfo对象,并赋值
				AppData appInfo = new AppData();
				appInfo.setAppLabel(appLabel);
				appInfo.setPkgName(pkgName);
				appInfo.setAppIcon(icon);
				appInfo.setIntent(launchIntent);
				list.add(appInfo); // 添加至列表中
				Log.i("list", "list数据");
			}
		}
	}
适配器中:

 
<pre name="code" class="java">public class ApplicationInfoAdapter extends BaseAdapter {
	private List<AppData> mlistAppInfo;
	private LayoutInflater infater;

	public ApplicationInfoAdapter(Context context, List<AppData> apps) {
		infater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mlistAppInfo = apps;
	}

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

	@Override
	public Object getItem(int position) {
		return mlistAppInfo.get(position);
	}

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

	@Override
	public View getView(int position, View convertview, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertview == null || convertview.getTag() == null) {
			convertview = infater.inflate(R.layout.browse_app_item, null);
			holder = new ViewHolder();
			holder.appIcon = (ImageView) convertview.findViewById(R.id.imgApp);
			holder.tvAppLabel = (TextView) convertview
					.findViewById(R.id.tvAppLabel);
			holder.tvPkgName = (TextView) convertview
					.findViewById(R.id.tvPkgName);
			convertview.setTag(holder);
		} else {
			holder = (ViewHolder) convertview.getTag();
		}
		AppData appInfo = (AppData) getItem(position);
		holder.appIcon.setImageDrawable(appInfo.getAppIcon());
		holder.tvAppLabel.setText(appInfo.getAppLabel());
		holder.tvPkgName.setText(appInfo.getPkgName());
		return convertview;
	}

	class ViewHolder {
		ImageView appIcon;
		TextView tvAppLabel;
		TextView tvPkgName;
	}
}
这样一个简单的获取已经安装的APP的图标的程序成功。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值