android获取已安装应用信息(列表图标包名列表)

参考博客:http://www.cnblogs.com/mainroadlee/archive/2011/05/23/android_get_installed_app.html

原文中的代码链接打不开,文中也没有说如何构建adapter,经过摸索,该小应用的MainActivity如下:

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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

class AppInfo{
    public String appName="";
    public String packageName="";
    public String versionName="";
    public int versionCode=0;
    public Drawable appIcon=null;
    public void print(){
        Log.v("app","Name:"+appName+" Package:"+packageName);
        Log.v("app","Name:"+appName+" versionName:"+versionName);
        Log.v("app","Name:"+appName+" versionCode:"+versionCode);
    }
}
public class MainActivity extends AppCompatActivity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try{
            List<AppInfo> appList=new ArrayList<>();
            List<PackageInfo> packages=getPackageManager().getInstalledPackages(0);
            for(int i=0;i<packages.size();++i){
                PackageInfo packageInfo=packages.get(i);
                AppInfo tmpInfo=new AppInfo();
                tmpInfo.appName=packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                tmpInfo.packageName=packageInfo.packageName;
                tmpInfo.versionName=packageInfo.versionName;
                tmpInfo.versionCode=packageInfo.versionCode;
                tmpInfo.appIcon=packageInfo.applicationInfo.loadIcon(getPackageManager());

                //获取非系统应用
                if((packageInfo.applicationInfo.flags& ApplicationInfo.FLAG_SYSTEM)==0){
                    appList.add(tmpInfo);
                }
                //appList.add(tmpInfo);
            }
            ListView listView=(ListView)findViewById(R.id.list);
            MyAdapter arrayAdapter=new MyAdapter(this,R.layout.list_item,appList);
            listView.setAdapter(arrayAdapter);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

class MyAdapter extends ArrayAdapter<AppInfo>{
    private Context mContext;
    private int resource;
    public MyAdapter(Context context,int resource,List<AppInfo> data){
        super(context,resource,data);
        this.mContext=context;
        this.resource=resource;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(mContext).inflate(resource,null);
            holder.image=(ImageView)convertView.findViewById(R.id.ItemImage);
            holder.title=(TextView)convertView.findViewById(R.id.ItemTitle);
            holder.text=(TextView)convertView.findViewById(R.id.ItemText);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }

        holder.image.setImageDrawable(getItem(position).appIcon);
        holder.title.setText(getItem(position).appName);
        holder.text.setText(getItem(position).packageName);

        return convertView;
    }
    final class ViewHolder{
        ImageView image;
        TextView title;
        TextView text;
    }
}

其中的布局文件activity_main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yidian.news.packinfo.MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

 list_item的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/ItemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ImageView>

    <TextView
        android:id="@+id/ItemTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ItemImage"
        android:textSize="25sp" >
    </TextView>

    <TextView
        android:id="@+id/ItemText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ItemImage"
        android:layout_below="@id/ItemTitle"
        android:textSize="15sp" >
    </TextView>
</RelativeLayout>



阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页