【问题标题】:need to run tasks only after Async task has finished只有在异步任务完成后才需要运行任务
【发布时间】:2012-10-09 12:31:33
【问题描述】:

如何确保异步任务在我运行某些任务之前完成。我需要在异步任务更改该变量的值之后使用一个变量。如果我在异步运行完成之前运行代码,那么我搞砸了。有什么帮助吗?我显然是异步任务的新手。如果您查看我的代码,我可能没有按预期使用 onPostExecute(),因此建议会有所帮助。我最初的想法是继续向异步任务添加东西,但我认为这只是不好的做法,因为我有很多东西必须串联运行。基本上,我认为它归结为:我如何确保 UI 线程中的任务在我的异步任务完成之前不会开始运行。

public class MainActivity extends MapActivity {
myJSONmap;

public void onCreate(Bundle savedInstanceState) {
new AsyncStuff().execute();
locatePlace(myJSONmap);

 class AsyncStuff extends AsyncTask<Void, Integer, JSONObject> {
            @Override
            protected JSONObject doInBackground(Void... params) {
                jObject = GooglePlacesStuff.getTheJSON(formatedURL);
                return null;
            }
            @Override
            protected void onPostExecute(JSONObject result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                myJSONmap = JSONextractor.getJSONHMArrayL(jObject);  // getting the parsed data from the JSON object.
                //the arraylist contains a hashmap of all the relevant data from the google website.
            }
        }

【问题讨论】:

    标签: java android multithreading android-asynctask http-post


    【解决方案1】:

    您可能想了解更多关于 Android Developer 上的 AsyncTask

    http://developer.android.com/intl/es/reference/android/os/AsyncTask.html

    关于提示,我个人的选择是将布尔值传递给 onPostExecute。这样您就可以评估 doInBackground 是否成功,然后确定要做什么(错误消息或更新布局)。

    请记住,在理想情况下,onPostExecute 方法应该更新屏幕,前提是您的数据正常。在您的示例中,为什么不包括

    myJSONmap = JSONextractor.getJSONHMArrayL(jObject);
    

    在doInBackground 上?然后调用

    locatePlace(myJSONmap);
    

    像这样:

    class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
        String errorMsg;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected Integer doInBackground(Void... v) {
            try{
                jObject = GooglePlacesStuff.getTheJSON(formatedURL);
                myJSONmap = JSONextractor.getJSONHMArrayL(jObject);
                //do stuff
                return true;
            } catch (JSONException e){
                errorMsg="Something wrong in the json";
                return false;
            }
    
        }
    
        @Override
        protected void onPostExecute(Boolean success) {
            if(success){
                locatePlace(myJSONmap);
                //update layout
            } else {
                //show error
            }
    
    
        }
    }
    

    【讨论】:

    • 所以基本上我所有剩余的活动都将在 onPostExecute() 方法中?这是个好习惯吗?
    • 是的,onPostExecute() 是任务完成之后需要做的事情。
    • onPostExecute() 中可能有很多行代码。这样可以吗?
    • 当然,假设您需要更新整个布局,这可能需要几行。或者创建一个对话框。您甚至可以在活动中调用另一个函数。但是请记住,如果您不需要在主线程中执行此操作,请将其放在 doInBackground() 中,这样您就不会阻塞主活动线程。如果可以,请尝试仅在 onPostExecute() 上保留布局更新。
    • 我想我现在明白了。所以简单总结一下:1.使用主线程启动ativity。 2.使用背景进行计算等。 3.对依赖后台线程完成的事情使用post execute
    【解决方案2】:

    你可以使用下面的代码来执行异步任务 -

    MyAsyncTask_a asyncTask_a = new MyAsyncTask_a();
                            asyncTask_a.execute();
    

    一旦 doInBackground() 任务完成,那么只有控制权会转到 postExecute()。 您不能在 doInBackground 中执行任何 UI 操作,但您可以在 preExecute() 和 postExecute() 中执行此操作。

    class MyAsyncTask_a extends AsyncTask<Void, Void, Integer> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Integer doInBackground(Void... arg0) {
                // TODO Auto-generated method stub
    
                return 1;
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
    
    
            }
        }
    

    希望这会对你有所帮助。

    【讨论】:

    • 这不是我已经在做的吗?我的问题是:如何确保 UI 线程中的任务在我的异步任务完成之前不会运行。
    • 把你想要发生的事情放在 onPostExecute 函数中。 IE 只需在 onPostExecute 中调用 locatePlace。
    • 把你想在 UI 线程中运行的所有东西放在 post execute..return 1 确保一旦 doInBackground 块结束,那么只有它会去 post execute 并在那里运行命令
    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多