AsyncTask 不锁定主线程?

发布于 2024-10-09 05:54:22 字数 1462 浏览 0 评论 0原文

我使用以下代码来填充自定义 ListPreference 对话框。由于填充过程需要很多时间,我想在填充过程中显示一个进度对话框。

我的问题是 Filler.execute() 不会阻止 onPrepareDialogBu​​ilder 并且函数会在填充值之前一直运行到最后导致异常...知道吗?

@Override
protected void onPrepareDialogBuilder(Builder builder) {        
    // Load data
    if (this.getEntries()==null) {
        FillerTask filler = new FillerTask();
        filler.execute();
    }   
    Log.d(TAG, "Filler finished");
    super.onPrepareDialogBuilder(builder);
}

这是 Filltertask 代码,基本上他会查找具有 MAIN Intent 的每个活动来填充列表:

private class FillerTask extends AsyncTask<Void, Void, String[][]> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        Log.d(TAG, "Dismiss dialog");
        dialog = ProgressDialog.show(MyListPreference.this.getContext(), "", "Doing stuff...", true);
    }

    @Override
    protected String[][] doInBackground(Void... params) {
        return fill();
    }

    public String[][] fill() {
    Log.d(TAG, "Fill started");

        CREATE LISTS...

        // Done
        Log.d(TAG, "Fill done");
        String[][] result = new String[][] {entryNames, entryValues};
        return result;
    }


    @Override
    protected void onPostExecute(String[][] result) {
        Log.d(TAG, "Post execute");
        MyListPreference.this.setEntries(result[0]);
        MyListPreference.this.setEntryValues(result[1]);
        dialog.dismiss();
    }
 }  

I'm using following code to fill a custom ListPreference dialog. Since the fill procedure takes a lot of time i want to show a progress dialog during the fill procedure.

My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?

@Override
protected void onPrepareDialogBuilder(Builder builder) {        
    // Load data
    if (this.getEntries()==null) {
        FillerTask filler = new FillerTask();
        filler.execute();
    }   
    Log.d(TAG, "Filler finished");
    super.onPrepareDialogBuilder(builder);
}

Here is Filltertask code, basically he looks for every activity with a MAIN Intent filling a list:

private class FillerTask extends AsyncTask<Void, Void, String[][]> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        Log.d(TAG, "Dismiss dialog");
        dialog = ProgressDialog.show(MyListPreference.this.getContext(), "", "Doing stuff...", true);
    }

    @Override
    protected String[][] doInBackground(Void... params) {
        return fill();
    }

    public String[][] fill() {
    Log.d(TAG, "Fill started");

        CREATE LISTS...

        // Done
        Log.d(TAG, "Fill done");
        String[][] result = new String[][] {entryNames, entryValues};
        return result;
    }


    @Override
    protected void onPostExecute(String[][] result) {
        Log.d(TAG, "Post execute");
        MyListPreference.this.setEntries(result[0]);
        MyListPreference.this.setEntryValues(result[1]);
        dialog.dismiss();
    }
 }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-10-16 05:54:22

我的问题是,filler.execute() 不会阻止 onPrepareDialogBu​​ilder,并且函数会在填充值之前一直运行到最后,从而导致异常...知道吗?

这就是 AsyncTask 背后的全部要点。 AsyncTask中的“Async”是异步的意思。

使用您的 AsyncTask 获取数据。然后,在 onPostExecute() 中显示该对话框。

My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?

That is the entire point behind an AsyncTask. The "Async" in AsyncTask means asynchronous.

Use your AsyncTask to get your data. Then, in onPostExecute(), display the dialog.

鹊巢 2024-10-16 05:54:22

找到解决方案,最好的方法是重写 onClick 方法并让 AsyncTask postExecute 调用“super()”,这样在加载内容并且在加载过程中正确显示进度条之前,不会传递单击。

Found the solution, best way to do this is override the onClick method and let the AsyncTask postExecute call the "super()", so click is not passed until content is loaded and during load progress bar is correctly displayed.

日久见人心 2024-10-16 05:54:22

asyntask 不会锁定主线程,它只是将消息放入主线程的消息队列中

asyntask doesn't lock main thread, it just drops a message to message queue of main thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文