Android--得到手机中所有应用信息的列表

得到手机中所有应用信息的列表,返回list< AppInfo>类型,直接创建实例类对象可以等于这个方法。配合BaseAdapter可以展现整个列表

    /**
     * 得到手机中所有应用信息的列表
     * AppInfo
     * Drawable icon 图片对象
     * String appName
     * String packageName
     * @return
     */
    protected List<AppInfo> getAllAppIofo(){
        List<AppInfo> list=new ArrayList<AppInfo>();

        //得到应用packageManager
        PackageManager packageManager=getPackageManager();

        //创建一个主界面intent
        Intent intent=new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        //得到包含应用信息列表
        List<ResolveInfo> ResolveInfos=packageManager.queryIntentActivities(intent,0);

        //遍历
        for(ResolveInfo ri:ResolveInfos){
            //得到包名
            String packageName=ri.resolvePackageName;
            //得到图标
            Drawable icon=ri.loadIcon(packageManager);
            //得到应用名称
            String appName=ri.loadLabel(packageManager).toString();
            //封装应用信息对象
            AppInfo appInfo=new AppInfo(icon,appName,packageName);
            //添加到list
            list.add(appInfo);
        }
        return list;
    }

案例:做一个显示所有应用列表的应用,点击其中某一个可以弹出应用名称,长按从表格中删除应用(不是硬盘删除)

	private ListView lv_main;
    private List<AppInfo> data;
    private AppAdapter adapter;
    //private GridView gv_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fragment);


        //gv_main=(GridView)findViewById(R.id.gv_main);
        lv_main=(ListView)findViewById(R.id.lv_main);
        adapter=new AppAdapter();
        data=getAllAppIofo();
        lv_main.setAdapter(adapter);

        //个体ListView设置Item的点击监听
        lv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             *
             * @param parent ListView
             * @param view  当前行的item视图对象
             * @param position  当前行下标
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String appName = data.get(position).getAppName();
                Toast.makeText(Fragment.this,appName,Toast.LENGTH_SHORT).show();
            }
        });
        lv_main.setOnItemLongClickListener(this);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        data.remove(position);
        //iv_main.setAdapter(adapter);//显示列表,不会使用缓存额、的
        adapter.notifyDataSetChanged();//通知更新列表,使用所有缓存的item的视图对象
        return true;
    }


    public class AppAdapter extends BaseAdapter{

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //1,加载格式布局
            if(convertView==null){
                convertView = View.inflate(Fragment.this, R.layout.item_main, null);
            }
            //2,得到当前行数据对象
            AppInfo appInfo = data.get(position);
            //3,得到需要更新的子对象
            ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_item_icon);
            TextView textView=(TextView)convertView.findViewById(R.id.tv_item_name);
            TextView packageName=(TextView)convertView.findViewById(R.id.tv_item_pn);
            //4,更新视图
            imageView.setImageDrawable(appInfo.getIcon());
            textView.setText(appInfo.getAppName());
            packageName.setText(appInfo.getPackageName());


            return convertView;
        }
    }



    /**
     * 得到手机中所有应用信息的列表
     * AppInfo
     * Drawable icon 图片对象
     * String appName
     * String packageName
     * @return
     */
    protected List<AppInfo> getAllAppIofo(){
        List<AppInfo> list=new ArrayList<AppInfo>();

        //得到应用packageManager
        PackageManager packageManager=getPackageManager();

        //创建一个主界面intent
        Intent intent= new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        //得到包含应用信息列表
        List<ResolveInfo> ResolveInfos=packageManager.queryIntentActivities(intent,0);

        //遍历
        for(ResolveInfo ri:ResolveInfos){


            //得到包名
            String packageName=ri.resolvePackageName;
            //得到图标
            Drawable icon=ri.loadIcon(packageManager);
            //得到应用名称
            String appName=ri.loadLabel(packageManager).toString();
            //封装应用信息对象
            AppInfo appInfo=new AppInfo(icon,appName,packageName);
            //添加到list
            list.add(appInfo);
        }
        return list;
    }

实体类属性:

	private Drawable icon;
    private String appName;
    private String packageName;

item布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_item_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_foreground"/>
    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_toRightOf="@+id/iv_item_icon"
        android:text="应用名称"

        />








RelativeLayout>

你可能感兴趣的:(Android)