【问题标题】:Android additional threads and LooperAndroid 附加线程和 Looper
【发布时间】:2016-05-23 19:20:14
【问题描述】:

我正在编写一个 Android 应用程序,它从 SQLite Database 读取数据,然后在下一个屏幕上显示数据。每当我对数据库进行查询时,我都会收到一条错误消息,指出主线程上正在完成太多工作。

然后我将我的查询放在一个新线程中:

        (new Thread()
        {
            public void run()
            {
                Looper.prepare();
                try
                {
                    FPJobCardWizard data = dbHelperInstance.loadFPJobCardWizardFull(fitmentHash);
                    wState.fitmentItemSet(data.fitmentItemGet());
                } catch (Exception e) {e.printStackTrace();}
                Looper.loop();
            }
        }).start();

现在 gui/main 线程在 Query 完成之前完成了它的操作,因此 data 变量仍然是空的。我阅读了一些帖子和 API 文档,似乎我需要使用 Looper(这似乎是正确的修复),但我从未使用过 Looper,似乎无法让它工作。

请您检查上面的代码并指导我正确的方向。

提前谢谢大家。

【问题讨论】:

  • 使用处理程序将数据发送到 UI 线程。

标签: android multithreading looper


【解决方案1】:

这里的最佳选择是使用AsyncTask,因为它可以让您在后台线程中执行所有后台工作,然后在生成结果时使用 UI 线程应用它:

所以,正如AsyncTask 的生命周期中所解释的那样,您可以在方法doInBackground() 中完成所有后台工作,然后在方法onPostExecute() 上完成所有UI 工作,该方法将在执行后执行根据生命周期,doInBackground() 方法的结果,如果您想更多地了解AsyncTask,请查看提供以下示例代码的this example

public class AsyncTaskTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        // This starts the AsyncTask
        // Doesn't need to be in onCreate()
        new MyTask().execute("my string paramater");
    }

    // Here is the AsyncTask class:
    //
    // AsyncTask<Params, Progress, Result>.
    //    Params – the type (Object/primitive) you pass to the AsyncTask from .execute() 
    //    Progress – the type that gets passed to onProgressUpdate()
    //    Result – the type returns from doInBackground()
    // Any of them can be String, Integer, Void, etc. 

    private class MyTask extends AsyncTask<String, Integer, String> {

        // Runs in UI before background thread is called
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // Do something like display a progress bar
        }

        // This is run in a background thread
        @Override
        protected String doInBackground(String... params) {
            // get the string from params, which is an array
            String myString = params[0];

            // Do something that takes a long time, for example:
            for (int i = 0; i <= 100; i++) {

                // Do things

                // Call this to update your progress
                publishProgress(i);
            }

            return "this string is passed to onPostExecute";
        }

        // This is called from background thread but runs in UI
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

            // Do things like update the progress bar
        }

        // This runs in UI when background thread finishes
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            // Do things like hide the progress bar or change a TextView
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 2013-09-17
    • 2013-11-15
    • 2012-01-16
    • 2014-10-03
    • 1970-01-01
    • 2011-04-22
    • 2012-02-04
    • 2018-05-20
    相关资源
    最近更新 更多