I am getting a list of installed non-system apps to show to the user and I am using this to do so:
private class getApplications extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// perform long running operation operation
for (i = 0; i < list.size(); i++) {
if ((list.get(i).flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
label = (String) pm.getApplicationLabel(list.get(i));
Log.w("Installed Applications", list.get(i).packageName.toString());
}
}
return label;
}
@Override
protected void onPostExecute(String result) {
txtApplications.append(label);
if (i!=list.size()-1) {
txtApplications.append(", ");
}
}
@Override
protected void onPreExecute() {
pm = getPackageManager();
list = pm.getInstalledApplications(PackageManager.GET_META_DATA);
}
};
This works fine as long as it is on the main UI, but it causes the app to lag when loading. I have read these three questions: AsyncTask and getInstalledPackages() fail, PackageManager.getInstalledPackages() returns empty list, Showing ProgressDialog during UI Thread operation in Android and I think understand what they are saying to do, but I am having problems understanding how to make it work. I know the delay is coming from List list = pm.getInstalledApplications(PackageManager.GET_META_DATA); and I know getInstalledApplications(PackageManager.GET_META_DATA); has to be run on the main UI or the app force closes. How do I go about keeping getInstalledApplications(PackageManager.GET_META_DATA); where it needs to be, but populate the list in the background so the app doesn't appear to be frozen? Thank you in advance for any help.
Updated code to show Asynctask. I got the code to run in an Async, but now it only shows one result in the textview instead of the list. I know it has to be some thing simple that I am missing to make it work.