Android how to wait for code to finish before continuing

前端 未结 5 1321
南笙
南笙 2021-01-17 21:28

I have a method called hostPhoto(); it basically uploads an image to a site and retrieves a link. I then have an other method to post the link to a website.

5条回答
  •  猫巷女王i
    2021-01-17 21:35

    You can use AsyncTask here,

    AsyncTask

    By Using that you can execute the code of

    hostPhoto()

    in doInBackground() and then execute the code of

    post(text+" "+link);

    in the onPostExecute() Method, that will the best solution for you.

    You can write the code in this pattern

    private class MyAsyncTask extends AsyncTask
    {
        @Override
        protected Void doInBackground(Void... params) {
            hostPhoto();
            return null;
        }
       @Override
       protected void onPostExecute(Void result) {
            post(text+" "+link);
        }
     }
    

    and the can execute it using

     new MyAsyncTask().execute();
    

提交回复
热议问题