Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有asynctask的getInstalledApplications()_Android_Android Asynctask - Fatal编程技术网

Android 带有asynctask的getInstalledApplications()

Android 带有asynctask的getInstalledApplications(),android,android-asynctask,Android,Android Asynctask,我正在获取一个已安装的非系统应用程序列表,以显示给用户,我正在使用此列表来执行此操作: private class getApplications extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { // perform long running operation operation

我正在获取一个已安装的非系统应用程序列表,以显示给用户,我正在使用此列表来执行此操作:

    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);

    }

};
私有类getApplications扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//执行长时间运行操作
对于(i=0;i
只要它在主UI上,它就可以正常工作,但它会导致应用程序在加载时延迟。我读过这三个问题:,我想我理解他们在说什么,但我在理解如何让它工作时遇到了问题。我知道延迟来自List=pm.getInstalledApplications(PackageManager.GET_META_DATA);我知道GetInstalledApplication(PackageManager.GET_META_DATA);必须在主UI上运行,否则应用程序强制关闭。如何保存GetInstalledApplication(PackageManager.GET_META_数据);它需要在哪里,但在后台填充列表,这样应用程序就不会被冻结?提前感谢您的帮助


更新代码以显示异步任务。我得到了异步运行的代码,但现在它只在textview中显示一个结果,而不是列表。我知道要让它工作起来,我必须做一些简单的事情。

我会修改你的
doInBackground()
,这样看起来:

@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) {

            //add all the application names to the same String.
            label +=", " + (String) pm.getApplicationLabel(list.get(i));


            Log.w("Installed Applications", list.get(i).packageName.toString());

        }
    }

    return label;
}

从现在起,
label
包含所有应用程序名称(因为
label+=…

如果
pm.getInstalledApplications()
需要在UI线程上,那么按照建议的答案之一,将其放入AsyncTask的
onPreExecute()
方法中。然后在
doInBackground()
中进行包解析(那个大循环)-列表应该是可用的。@A-C,谢谢,这帮我弄清楚了。不过我还有一个问题。现在,在异步任务中,它只在textview而不是列表中显示一个结果。尽管如此,日志仍然显示所有结果。我认为不需要在UI线程上执行它,请参见此处:已接受。谢谢你告诉我需要在哪里做改变。
    @Override
    protected void onPostExecute(String result) {

        txtApplications.append(label);

           }
    }