简体   繁体   中英

Populate ListPreference with installed applications

我一直在开发一个应用程序,我想选择其中一个已安装的应用程序,然后只需要将其(“应用程序”程序包)存储在ListPreference中,但似乎找不到有效的解决方案,任何帮助都将非常有用。

You could use PackageManager.getInstalledApplications method to get a list of installed Applications.

And use:

  1. PackageManager.getApplicationLabel(ApplicationInfo) to get the Labels for ListPreference.setEntries .

  2. ApplicationInfo.packageName to get the package name for ListPreference.setEntryValues .

As shown here:

PackageManager pm = getPackageManager();
List<ApplicationInfo> appList = pm.getInstalledApplications(
        PackageManager.GET_META_DATA);

int count = appList.size();
String[] appLabels = new String[count];
String[] pkgNames = new String[count];

for (int i = 0; i < count ; i++) {
    ApplicationInfo app = appList.get(i);
    appLabels[i] = pm.getApplicationLabel(app).toString();
    pkgNames[i] = app.packageName;
}

prefList.setEntries(appLabels);
prefList.setEntryValues(pkgNames);

Then you can retrieve package name for any item and use PackageManager.getApplicationInfo to get ApplicationInfo object of that package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM