获取资源

时间:2015-11-26 08:07:25

标签: android asynchronous android-asynctask logcat android-package-managers

我正在尝试获取设备ThirdParty和System上的所有应用程序的列表,我在AsyncTask中运行此操作,当应用程序打开任务运行时,我看到在此操作期间获取此列表时的长操作看到Logcat中打印出的警告,我认为这是长时间操作背后的原因:

11-26 09:51:23.720 1739-1846/com.example.AppList W/ResourceType: No package identifier when getting value for resource number 0x00000000
11-26 09:51:23.720 1739-1846/com.example.AppList W/PackageManager: Failure retrieving resources for com.google.android.onetimeinitializer: Resource ID #0x0
11-26 09:51:23.870 1739-1846/com.example.AppList W/ResourceType: No package identifier when getting value for resource number 0x00000000
11-26 09:51:23.870 1739-1846/com.example.AppList W/PackageManager: Failure retrieving resources for com.qualcomm.shutdownlistner: Resource ID #0x0
11-26 09:51:23.994 1739-1846/com.example.AppList W/ResourceType: No package identifier when getting value for resource number 0x00000000
11-26 09:51:23.994 1739-1846/com.example.AppList W/PackageManager: Failure retrieving resources for com.android.wallpapercropper: Resource ID #0x0
11-26 09:51:24.744 1739-1846/com.example.AppList W/ResourceType: No package identifier when getting value for resource number 0x00000000
11-26 09:51:24.744 1739-1846/com.example.AppList W/PackageManager: Failure retrieving resources for com.android.documentsui: Resource ID #0x0
11-26 09:51:24.873 1739-1846/com.example.AppList W/ResourceType: No package identifier when getting value for resource number 0x00000000
11-26 09:51:24.874 1739-1846/com.example.AppList W/PackageManager: Failure retrieving resources for com.android.externalstorage: Resource ID #0x0
                                      ....
                                      ....
/** 
 * I got all installed and system apps on the device
 * listed as the above in the logcat as Warning.
 */
                                      ....
                                      .... to the end.

这是doInBackground代码:

@Override
    protected final List<App> doInBackground(List<App>... params) {

        PackageManager pm = getActivity().getPackageManager();
        List<ApplicationInfo> appsList = pm.getInstalledApplications(0);
        List<PackageInfo> packList = pm.getInstalledPackages(0);
        List<App> userApps = new ArrayList<>();
        int totalApp = appsList.size();

        for (int i = 0; i < totalApp; i++) {
            ApplicationInfo info = appsList.get(i);
            final App app = new App();

            app.setIcon(info.loadIcon(getActivity().getPackageManager()));
            app.setName(info.loadLabel(getActivity().getPackageManager()).toString());
            app.setPackageName(info.packageName);

            if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                userApps.add(app);
            }
        }
        return userApps;
    }

那么如何解决这个问题,或者只是如何修复长时间操作?

修改

我尝试从列表中删除应用程序图标功能,它的加载速度比附加图像时快,因此图像中的问题

@David Corsalini在comments中告诉我:

  

在后台线程中加载位图,从holder.bind()方法调用此后台线程。我没有这个代码。

我正在使用ListViewViewHolder,但不了解holder.bind(),那么如何实现这一目标呢?!

1 个答案:

答案 0 :(得分:1)

加载应用程序图标需要很长时间,因为您将所有应用程序图标作为可绘制内容存储在一个大列表中 - 这不是一个好主意,除非您必须这样做。相反,我建议删除那个&#34; icon&#34;您的App类中的变量,并在列表适配器中加载图标。在列表适配器的getView方法中,您可以将图标drawables设置如下:

    Drawable icon;
    try {
        icon = context.getPackageManager().getApplicationIcon(packageName);
    } catch (PackageManager.NameNotFoundException ex) {
        Log.e(TAG, "Error", ex);
        icon = context.getResources().getDrawable(R.drawable.icon_default);
    }

    holder.imageview.setImageDrawable(icon);

我不知道您是如何实现适配器的,但我的建议是提供您在后台任务中收集的App对象列表,并提供这些App对象的包名称。希望它有所帮助。