android获取软件列表

这个demo实现了一个软件列表的功能 
这个是截图 有需要的朋友可以参考一下:
返回软件列表.png 

MainActivity
  1. package com.sunjialiang;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;

  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.pm.PackageInfo;
  9. import android.content.pm.PackageManager;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.provider.Settings;
  13. import android.view.View;
  14. import android.view.View.OnLongClickListener;
  15. import android.widget.Adapter;
  16. import android.widget.AdapterView;
  17. import android.widget.AdapterView.OnItemLongClickListener;
  18. import android.widget.ListAdapter;
  19. import android.widget.ListView;
  20. /**

  21. * 实现获取软件的程序名称 包名 以及图标*/
  22. public class MainActivity extends Activity implements OnItemLongClickListener{
  23.         private ListView lv;
  24.         Adapter adapter;
  25.         ArrayList<HashMap<String, Object>> items=new ArrayList<HashMap<String, Object>>();

  26.         @Override
  27.         public void onCreate(Bundle savedInstanceState) {
  28.                 super.onCreate(savedInstanceState);
  29.                 setContentView(R.layout.main);
  30.                 lv = (ListView) findViewById(R.id.lv);
  31.                 PackageManager  pm= getPackageManager();
  32.                 //得到PackageManager对象
  33.                 List<PackageInfo> packs = pm.getInstalledPackages(0);
  34.                 //得到系统 安装的所有程序包的PackageInfo对象

  35.                 for (PackageInfo pi : packs) {
  36.                         HashMap<String, Object> map = new HashMap<String, Object>();
  37.                         map.put("icon", pi.applicationInfo.loadIcon(pm));
  38.                         //图标
  39.                         map.put("appName", pi.applicationInfo.loadLabel(pm));
  40.                         //应用名
  41.                         map.put("packageName", pi.packageName);
  42.                         //包名
  43.                         items.add(map);
  44.                         //循环读取存到HashMap,再增加到ArrayList.一个HashMap就是一项
  45.                 }

  46.                 adapter = new adapter(this, items, R.layout.piitem, new String[] {
  47.                                 "icon", "appName", "packageName" }, new int[] { R.id.icon,
  48.                                 R.id.appName, R.id.packageName });
  49.                 //参数:Context,ArrayList(item的集合),item的layout,包含ArrayList中Hashmap的key的数组,key所对应的值相对应的控件id
  50.                  lv.setAdapter((ListAdapter) adapter);

  51.         }

  52.         @Override
  53.         public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position,
  54.                         long id) {
  55.                 
  56.                 
  57.                 return false;
  58.         }



  59. }
复制代码
适配器
  1. package com.sunjialiang;

  2. import java.util.List;
  3. import java.util.Map;
  4. import android.content.Context;
  5. import android.graphics.drawable.Drawable;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ImageView;
  10. import android.widget.SimpleAdapter;
  11. import android.widget.TextView;

  12. public class adapter extends SimpleAdapter {
  13.         private int[] mTo;
  14.     private String[] mFrom;
  15.     private ViewBinder mViewBinder;
  16.     private List<? extends Map<String, ?>> mData;
  17.     private int mResource;
  18.     private LayoutInflater mInflater;

  19.         public adapter(Context context, List<? extends Map<String, ?>> data,
  20.                         int resource, String[] from, int[] to) {
  21.                 super(context, data, resource, from, to);
  22.                 mData = data;
  23.         mResource = resource;
  24.         mFrom = from;
  25.         mTo = to;
  26.         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  27.         }
  28.         public View getView(int position, View convertView, ViewGroup parent) {
  29.         return createViewFromResource(position, convertView, parent, mResource);
  30.     }
  31.     private View createViewFromResource(int position, View convertView,
  32.             ViewGroup parent, int resource) {
  33.         View v;
  34.         if (convertView == null) {
  35.             v = mInflater.inflate(resource, parent, false);

  36.             final int[] to = mTo;
  37.             final int count = to.length;
  38.             final View[] holder = new View[count];

  39.             for (int i = 0; i < count; i++) {
  40.                 holder[i] = v.findViewById(to[i]);
  41.             }
  42.             v.setTag(holder);
  43.         } else {
  44.             v = convertView;
  45.         }
  46.         bindView(position, v);
  47.         return v;
  48.     }

  49.     private void bindView(int position, View view) {
  50.         final Map dataSet = mData.get(position);
  51.         if (dataSet == null) {
  52.             return;
  53.         }

  54.         final ViewBinder binder = mViewBinder;
  55.         final View[] holder = (View[]) view.getTag();
  56.         final String[] from = mFrom;
  57.         final int[] to = mTo;
  58.         final int count = to.length;

  59.         for (int i = 0; i < count; i++) {
  60.             final View v = holder[i];
  61.             if (v != null) {
  62.                 final Object data = dataSet.get(from[i]);
  63.                 String text = data == null ? "" : data.toString();
  64.                 if (text == null) {
  65.                     text = "";
  66.                 }

  67.                 boolean bound = false;
  68.                 if (binder != null) {
  69.                     bound = binder.setViewValue(v, data, text);
  70.                 }

  71.                 if (!bound) {
  72.                         //自定义适配器,关键在这里,根据传过来的控件类型以及值的数据类型,执行相应的方法
  73.                         //可以根据自己需要自行添加if语句。另CheckBox等继承自TextView的控件也会被识别成TextView, 这就需要判断值的类型了
  74.                   if (v instanceof TextView) {
  75.                           //如果是TextView控件
  76.                         setViewText((TextView) v, text);
  77.                         //调用SimpleAdapter自带的方法,设置文本
  78.                     } else if (v instanceof ImageView) {//如果是ImageView控件
  79.                         setViewImage((ImageView) v, (Drawable) data); 
  80.                         //调用下面自己写的方法,设置图片
  81.                     } else {
  82.                         throw new IllegalStateException(v.getClass().getName() + " is not a " +
  83.                                 " view that can be bounds by this SimpleAdapter");
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }

  89.     public void setViewImage(ImageView v, Drawable value) {
  90.             v.setImageDrawable(value);

  91.     }


  92. }
复制代码
模板文件.xml
piitem.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="horizontal" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.     <ImageView android:layout_width="48dip" android:id="@+id/icon" android:layout_height="48dip"></ImageView>
  6.         <LinearLayout android:orientation="vertical"
  7.                 android:layout_width="fill_parent" 
  8.                 android:layout_height="wrap_content">
  9.                 <TextView android:id="@+id/appName" 
  10.                           android:layout_width="fill_parent"
  11.                           android:layout_height="wrap_content" />
  12.                 <TextView android:id="@+id/packageName" 
  13.                           android:layout_width="fill_parent"
  14.                               android:layout_height="wrap_content" />
  15.         </LinearLayout>
  16. </LinearLayout>
复制代码
主xml里面只是一个ListView  。

转载于:https://my.oschina.net/limbusnet/blog/75466

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值