获取系统中所有应用数据

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView listView = new ListView(this);

        List> data = getData();

        SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2,
                new String[]{"appName", "pkgName"},
                new int[]{android.R.id.text1, android.R.id.text2});
        listView.setAdapter(adapter);

        setContentView(listView);
    }

    /**
     * 获取系统中所有应用数据
     *
     * @return 系统中所有应用数据列表
     */
    private List> getData() {
        List> data = new ArrayList<>();
        List packageInfos = getPackageManager().getInstalledPackages(0);
        for (PackageInfo packageInfo : packageInfos) {
            HashMap map = new HashMap<>(16);
            map.put("appName", packageInfo.applicationInfo.loadLabel(getPackageManager()).toString());
            map.put("pkgName", packageInfo.packageName);
            //以下三个为扩展数据
            map.put("versionName", packageInfo.versionName);
            map.put("versionCode", packageInfo.versionCode);
            map.put("drawable", packageInfo.applicationInfo.loadIcon(getPackageManager()));
            data.add(map);
        }
        return data;
    }
}
微信图片_20180905213734.jpg

你可能感兴趣的:(获取系统中所有应用数据)