AsyncTask的和getInstalledPackages()失败 [英] AsyncTask and getInstalledPackages() fail
问题描述
我真的不明白发生了什么,也许你可以给我一个想法。我为了让所有用户安装的应用程序及其权限的列表使用getInstalledPackages()。下面是我如何做到这一点:
I don't really understand what is happening and maybe you can give me an idea. I use getInstalledPackages() in order to get a list of all user installed applications and their permissions. Here is how I do it:
private PackageManager pm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pm = getPackageManager();
getListData();
}
private void getListData()
{
backTask = new BackTask();
backTask.execute();
}
protected class BackTask extends AsyncTask<Context, String, ArrayList<App>>
{
....
@Override
protected ArrayList<App> doInBackground(Context... param)
{
try {
//get a list of installed apps.
List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS);
for (PackageInfo packageInfo : packages) {
ApplicationInfo application = packageInfo.applicationInfo;
....
}
我需要刷新显示基于用户交互列表。问题出现在此行中的AsyncTask:
I need to refresh the list displayed based on user interaction. The problem appears at this line in AsyncTask:
List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS);
有时候包包含了所有的安装的应用程序有时是空的。比方说,例如开出3次,第3次是空的。有鉴于此code OUT的AsyncTask,并把它放在getListData()的作品没有任何问题,每一次。问题是,我得到一点点冻结,因为操作不上一回线。 那么什么是处理AsyncTask的?难道我做错了什么?
Sometimes packages contains all the installed apps sometimes is EMPTY. Let's say for example out of 3 times, the 3rd time is empty. Taking this code out of AsyncTask and put it on getListData() works every single time without any problems. The issue is that I get a little freeze because operation is not on a back thread. So what's the deal with AsyncTask ? Am I doing something wrong ?
推荐答案
签出相似,所以问题<一href="http://stackoverflow.com/questions/3455781/packagemanager-getinstalledpackages-returns-empty-list">here, CommonsWare认为,这种方法需要参与的UI线程,就我所看到的,这是唯一合理的解释。
Check out a similar SO question here, CommonsWare argues that this method need to be involved in UI thread, As far as I can see, this is the only reasonable explanation.
这篇关于AsyncTask的和getInstalledPackages()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!