Reputation: 5259
My main application does this: It retrievs data from the internet and has 3 button, when OnClicked, i am going to 3 other screens. because the data loading may be a little slow, I want to use an async Task. This is my sample code for asynctask.
class LoginProgressTask extends AsyncTask {
@Override
protected Boolean doInBackground(String... params) {
try {
Thread.sleep(4000); // Do your real work here
} catch (InterruptedException e) {
e.printStackTrace();
}
return Boolean.TRUE; // Return your real result here
}
@Override
protected void onPreExecute() {
showDialog(AUTHORIZING_DIALOG);
}
@Override
protected void onPostExecute(Boolean result) {
// result is the value returned from doInBackground
removeDialog(AUTHORIZING_DIALOG);
}
}
and this is my sample of my main activity:
public class MainScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainTheme();
}
public void MainTheme(){
retrieve_data(); //function for getting the data
... action with the buttons, onClicks Listener
}
}
My question is how can I mix those codes in One activity to make it work, becuase I haven't understood AsyncTask. Or what I should return in the doInBackground?
Upvotes: 1
Views: 5965
Reputation: 30276
For example, if you have a Button
to login, you should do something like this:
Button button; //Here button to go other sreen
public void onCreate(){
//some business code here
//notice: you have declare you button to your layout. I don't post it, but maybe you know how to
button.setOnClickListener(new OnClickListener){
@Override
public void onClick(View view){
LoginProcessTask loginTask = new LoginProcessTask(this);
login.excute(data of param1);
}
}
}
And you should notice that, in your LoginProcessTask
, you have wrongly extended it. It must be (just for example):
class LoginProgressTask extends AsyncTask<String, Integer, Integer>{ ......}
Upvotes: 0