1. 程式人生 > >獲取手機內的所有應用列表

獲取手機內的所有應用列表

效果圖:

activity:

public class AppInfoActivity extends AppCompatActivity {

    ListView listView;
    List<AppInfo> mList = new ArrayList<>();
    AppInfoAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_info);

        getData();
        listView = (ListView) findViewById(R.id.activity_app_info_list);
        mAdapter = new AppInfoAdapter(this,mList);
        listView.setAdapter(mAdapter);
    }

    private void getData() {
        List<PackageInfo> packageInfos = getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packageInfos.size(); i++) {
            PackageInfo packageInfo = packageInfos.get(i);
            //遮蔽掉系統應用
            if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0)
            {
                continue;
            }

            AppInfo appInfo = new AppInfo();
            appInfo.setAppName(packageInfo.applicationInfo.loadLabel(getPackageManager()).toString());
            appInfo.setPackageName(packageInfo.packageName);
            appInfo.setVersionCode(packageInfo.versionCode);
            appInfo.setVersionName(packageInfo.versionName);
            appInfo.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));

            mList.add(appInfo);
        }
    }
}


xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

Adapter:

public class AppInfoAdapter extends BaseAdapter {

    private List<AppInfo> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public AppInfoAdapter(Context context,List<AppInfo> list)
    {
        this.mContext = context;
        this.mList = list;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        AppInfo appInfo = mList.get(position);

        View view = null;
        AppInfoHolder appInfoHolder;
        if (convertView == null)
        {
            view = mLayoutInflater.inflate(R.layout.item_app_info,parent,false);
            appInfoHolder = new AppInfoHolder();
            appInfoHolder.imageView = (ImageView) view.findViewById(R.id.item_image);
            appInfoHolder.textView = (TextView) view.findViewById(R.id.item_name);

            view.setTag(appInfoHolder);
            convertView = view;
        }else {
            appInfoHolder = (AppInfoHolder) convertView.getTag();
        }

        appInfoHolder.imageView.setImageDrawable(appInfo.getIcon());
        appInfoHolder.textView.setText(appInfo.getAppName());

        return convertView;
    }

    private class AppInfoHolder{
        ImageView imageView;
        TextView textView;
    }
}

xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/item_name"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

</LinearLayout>

實體類:
public class AppInfo {
    private String appName = "";
    private String packageName = "";
    private String versionName = "";
    private int versionCode = 0;
    private Drawable icon;

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }
}