Android附加线程和Looper

时间:2016-05-23 10:54:49

标签: android multithreading looper

我正在编写一个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,似乎无法让它工作。

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

提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

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

AsyncTask life cycle

因此,正如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
        }
    }
}