遍历系统中所有应用程序.
使用到的知识点:
ListView,SimpleAdapter,PackageManager的使用.
运行效果图:
package com.sg7.applicationlist; import android.content.pm.PackageInfo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listView = new ListView(this); List<Map<String, Object>> 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<Map<String, Object>> getData() { List<Map<String, Object>> data = new ArrayList<>(); List<PackageInfo> packageInfos = getPackageManager().getInstalledPackages(0); for (PackageInfo packageInfo : packageInfos) { HashMap<String, Object> map = new HashMap<>(); 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("versionCode", packageInfo.applicationInfo.loadIcon(getPackageManager())); data.add(map); } return data; } }
源码下载:http://download.csdn.net/detail/lingwu7/9369576
欢迎朋友们加入中国安卓开发者群:146844427