欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

显示所有应用程序列表,点击进入 博客分类: android android应用连接跳转 

程序员文章站 2024-03-14 22:31:23
...
package com.example.likeqq;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	ListView lv;
	SimpleAdapter adapter;
	ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
	PackageManager pm;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		lv = (ListView) findViewById(R.id.listview);
		final PackageManager pm = getPackageManager();
		// 得到PackageManager对象
		List<PackageInfo> packs = pm
				.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
		// 得到系统 安装的所有程序包的PackageInfo对象
 
		for (PackageInfo pi : packs) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("icon", pi.applicationInfo.loadIcon(pm));
			// 图标
			map.put("appName", pi.applicationInfo.loadLabel(pm));
			// 应用名
			map.put("packageName", pi.packageName);
			// 包名
			items.add(map);
			// 循环读取存到HashMap,再增加到ArrayList.一个HashMap就是一项
		}
 
		adapter = new SimpleAdapter(this, items, R.layout.piitem, new String[] {
				"icon", "appName", "packageName" }, new int[] { R.id.icon,
				R.id.appName, R.id.packageName });
		// 参数:Context,ArrayList(item的集合),item的layout,包含ArrayList中Hashmap的key的数组,key所对应的值相对应的控件id
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
 
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String packageName = (String) items.get(position).get(
						"packageName");
				//取到点击的包名
				Intent i = pm.getLaunchIntentForPackage(packageName);
				//如果该程序不可启动(像系统自带的包,有很多是没有入口的)会返回NULL
				if (i != null)
					startActivity(i);
			}
 
		});
 
	}

}

布局文件就不添加了,都是listView的基础布局