简体   繁体   中英

Using AsyncTask to update UI

I have problem when implementing AsyncTask . I have to rotate my phone in order to get a recent information. Below is my class:

GamerObject gamer;
….

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ip = "134.188.204.155";
      // Set the name of the gamer
      gamername = (TextView) findViewById(R.id.gamer_name);
      // Set the gamerstatus:
      gamerstatus = (TextView) findViewById(R.id.tvgamer_status_msg);
      // set the job status
      jobstatus = (TextView) findViewById(R.id.tvJob_status_msg);
      new Operation().execute();
 }

private class Operation extends AsyncTask<Void, Void, Void> {

     @Override
     protected Void doInBackground(Void... params) {
        gamer= new GamerObject(ip);
        gamer.UpdateAllData();
     }

    @Override
    protected void onPostExecute(Void result) {
       updateUI();
    }
}

private void updateUI() {
    gamer.updateAllData();
    // Set the name of the gamer
    TextView gamername = (TextView) findViewById(R.id.gamer_name );
    gamername.setText(gamer.gamername);
    gamername = gamer.gamername ;
    // Set the gamer status:
     …
    // set the job status
     …
}

Before I was using a Intent for Refresh the Interface, but now I want to try using AsyncTask so that it can compatible with Android version 4. Anybody knows how to get the information updated without rotating the phone ?

EDIT:
If I'm not wrong, Why my UI didn't refresh if there is new value, it is because new Operation().execute(); only use once in onCreate. AsyncTask will be executed if onCreate has been called, which means every time I rotate my phone, it will go to onCreate . Anybody knows how to keep the AsyncTask executed?

AsyncTask will only be executed once, so whenever system calls onCreate on your activity dueto some lifecycle event, asyncTask will be executed.

One, simple but naive approach, would be to create a new Thread and use Handler to update your UI. Some more information can be found eg. here and of course in Android doc .

Better approach, but more complicated would be to use Loader and LoaderCallback along with ContentProvider as @metter mentioned. This will require implementing you own ContentProvider and force you to add more "abstraction layers" to your app but will allow to separate network base code and ui code.

So this is as always tough decision to make either use "simple" but ugle solution with threads or "harder" but elegant solution with ContentProvier

I want to get the inofrmation updated automatically without have to rotating the phone.

Doing that requires some more work. Unfortunately, we can't see what your Async task actually does. If it is reading data from a database and you wan't your UI to be informed about any changes to the database, then your content resolver could call it's notifyChange and your Activity would listen to these changes and then call the async task again. For that, you would use a Content Observer . However, if your task is downloading data from the web, then there are two methods to get informed if the data online changed. One is called polling and means that you periodically connect and check the server. You should never do that on a mobile device due to limitations in battery, performance and data traffic. The other is called pushing and requires you to set up some infrastructure .

I hope that helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM