获取资源值时没有包标识符
No package identifier when getting value for resource
我正在尝试获取第三方和系统设备上所有应用程序的列表,我 运行 在 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 中告诉我:
Load the bitmap in a background thread, call this background thread from the holder.bind()
method. I don't have the code for this one.
我正在使用 ListView
和 ViewHolder
,但不知道 holder.bind()
,请问如何实现?!
加载应用程序图标需要很长时间,因为您将所有应用程序图标作为可绘制对象存储在一个大列表中 - 这不是一个好主意,除非您必须这样做。相反,我建议在您的 App class 中删除 "icon" 变量并在您的列表适配器中加载该图标。在列表适配器的 getView 方法中,您可以如下设置图标可绘制对象:
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 对象的包名称。希望对你有帮助。
我正在尝试获取第三方和系统设备上所有应用程序的列表,我 运行 在 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 中告诉我:
Load the bitmap in a background thread, call this background thread from the
holder.bind()
method. I don't have the code for this one.
我正在使用 ListView
和 ViewHolder
,但不知道 holder.bind()
,请问如何实现?!
加载应用程序图标需要很长时间,因为您将所有应用程序图标作为可绘制对象存储在一个大列表中 - 这不是一个好主意,除非您必须这样做。相反,我建议在您的 App class 中删除 "icon" 变量并在您的列表适配器中加载该图标。在列表适配器的 getView 方法中,您可以如下设置图标可绘制对象:
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 对象的包名称。希望对你有帮助。