Hãy thử cách này: Tạo một lớp được gọi là PackageInformation
:
public class PackageInformation {
private Context mContext;
public PackageInformation(Context context) {
mContext = context;
}
class InfoObject {
public String appname = "";
public String pname = "";
public String versionName = "";
public int versionCode = 0;
public Drawable icon;
public void InfoObjectAggregatePrint() { //not used yet
Log.v(appname, appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
}
private ArrayList <InfoObject> getPackages() {
ArrayList <InfoObject> apps = getInstalledApps(false);
final int max = apps.size();
for (int i = 0; i < max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
public ArrayList <InfoObject> getInstalledApps(boolean getSysPackages) {
ArrayList <InfoObject> res = new ArrayList <InfoObject>();
List <PackageInfo> packs = mContext.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
InfoObject newInfo = new InfoObject();
newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mContext.getPackageManager());
res.add(newInfo);
}
return res;
}
}
Tuck này đi đâu đó và bây giờ để truy cập thông tin từ lớp Hoạt động làm việc của bạn làm điều này:
PackageInformation androidPackagesInfo = new PackageInformation(this);
ArrayList <InfoObject> appsData = androidPackagesInfo.getInstalledApps(true);
for (InfoObject info: appsData) {
Toast.makeText(MainActivity.this, info.appname, 2).show();
Drawable somedrawable = info.icon;
}
Vì Android 3.0 bạn có thể muốn có biểu tượng trình khởi chạy lớn hơn mà bạn không thể có được cách bạn mô tả. Nếu có, có lẽ câu trả lời của tôi cho câu hỏi dưới đây có thể giúp bạn: http://stackoverflow.com/questions/4600740/getting-app-icon-in-android/16279240#16279240 –