这个是截图 有需要的朋友可以参考一下:
![返回软件列表.png 返回软件列表.png](http://122.70.156.176/attachment_remote/forum/201108/04/1722565393u4nb59h4zwb4.png)
MainActivity
- package com.sunjialiang;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.view.View;
- import android.view.View.OnLongClickListener;
- import android.widget.Adapter;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemLongClickListener;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- /**
- *
- * 实现获取软件的程序名称 包名 以及图标*/
- public class MainActivity extends Activity implements OnItemLongClickListener{
- private ListView lv;
- Adapter adapter;
- ArrayList<HashMap<String, Object>> items=new ArrayList<HashMap<String, Object>>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- lv = (ListView) findViewById(R.id.lv);
- PackageManager pm= getPackageManager();
- //得到PackageManager对象
- List<PackageInfo> packs = pm.getInstalledPackages(0);
- //得到系统 安装的所有程序包的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 adapter(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((ListAdapter) adapter);
- }
- @Override
- public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position,
- long id) {
-
-
- return false;
- }
- }
- package com.sunjialiang;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- public class adapter extends SimpleAdapter {
- private int[] mTo;
- private String[] mFrom;
- private ViewBinder mViewBinder;
- private List<? extends Map<String, ?>> mData;
- private int mResource;
- private LayoutInflater mInflater;
- public adapter(Context context, List<? extends Map<String, ?>> data,
- int resource, String[] from, int[] to) {
- super(context, data, resource, from, to);
- mData = data;
- mResource = resource;
- mFrom = from;
- mTo = to;
- mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent, mResource);
- }
- private View createViewFromResource(int position, View convertView,
- ViewGroup parent, int resource) {
- View v;
- if (convertView == null) {
- v = mInflater.inflate(resource, parent, false);
- final int[] to = mTo;
- final int count = to.length;
- final View[] holder = new View[count];
- for (int i = 0; i < count; i++) {
- holder[i] = v.findViewById(to[i]);
- }
- v.setTag(holder);
- } else {
- v = convertView;
- }
- bindView(position, v);
- return v;
- }
- private void bindView(int position, View view) {
- final Map dataSet = mData.get(position);
- if (dataSet == null) {
- return;
- }
- final ViewBinder binder = mViewBinder;
- final View[] holder = (View[]) view.getTag();
- final String[] from = mFrom;
- final int[] to = mTo;
- final int count = to.length;
- for (int i = 0; i < count; i++) {
- final View v = holder[i];
- if (v != null) {
- final Object data = dataSet.get(from[i]);
- String text = data == null ? "" : data.toString();
- if (text == null) {
- text = "";
- }
- boolean bound = false;
- if (binder != null) {
- bound = binder.setViewValue(v, data, text);
- }
- if (!bound) {
- //自定义适配器,关键在这里,根据传过来的控件类型以及值的数据类型,执行相应的方法
- //可以根据自己需要自行添加if语句。另CheckBox等继承自TextView的控件也会被识别成TextView, 这就需要判断值的类型了
- if (v instanceof TextView) {
- //如果是TextView控件
- setViewText((TextView) v, text);
- //调用SimpleAdapter自带的方法,设置文本
- } else if (v instanceof ImageView) {//如果是ImageView控件
- setViewImage((ImageView) v, (Drawable) data);
- //调用下面自己写的方法,设置图片
- } else {
- throw new IllegalStateException(v.getClass().getName() + " is not a " +
- " view that can be bounds by this SimpleAdapter");
- }
- }
- }
- }
- }
- public void setViewImage(ImageView v, Drawable value) {
- v.setImageDrawable(value);
- }
- }
piitem.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView android:layout_width="48dip" android:id="@+id/icon" android:layout_height="48dip"></ImageView>
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView android:id="@+id/appName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <TextView android:id="@+id/packageName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </LinearLayout>