Is it possible to call another fumction from inside AsyncTask?
I have an AsyncTask that retrevies data from the server. What I need is to show the data retrevied in a dialog. How can I call the function to create the dialog from within the AsyncTask? If I can't do that, then what options do I have? Is it possible to know if an AsyncTask has been finished or not?
Here is my code:
//Function calling the AsyncTask
public void getData(int pos)
{
new DBTask().execute();
//Is it possible to know here if the AsyncTask has ended or not??
}
private class DBTask extends AsyncTask<Long, Boolean, Integer>{
ProgressDialog ServerPD = new ProgressDialog(MRRankingActivity.this);
@Override
protected void onPreExecute()
{
ServerPD = ProgressDialog.show (MRRankingActivity.this, "", "Connecting to server...", true, false);
}//End of onPreExecute
@Override
protected Integer doInBackground(Long... params)
{
publishProgress(isOnline());
if(isOnline())
{
getDBData();
if(isOK)
{
//I want to show the result by calling this function..
showResult();
//Calling this function will trow error..So how can i show my result??
Log.d("LIST","DONE..Reached Final Destination");
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Boolean... isConnection) {
// TODO Auto-generated method stub
super.onProgressUpdate(isConnection);
if(isOnline())
{
ServerPD.setMessage("Retreving Data");
}
}
@Override
protected void onPostExecute(Integer result) {
if (ServerPD.isShowing())
{
ServerPD.dismiss();
}
}//End of onPostExecute
}//End of DBTask
public void getDBData()
{
try{
//Code foe connecting to database and retreive data.
//If everything is ok then isOK is set to True..
//else its set to false
isOK=true;
}
catch(Exception e)
{
isOK=false;
}//End if catch
}
public void 开发者_高级运维showResult()
{
//I want to show result here in a new Dialog ...
}
//Checking is connection available
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
How can I show the result in this situation? Is there any method to find if AsyncTask has ended?
Simplifying what Mathew suggested..just modify your code like this First modify doInBackgroud by initialising an int varable isSuccess and then changing the value of isSucces to 1 inside the isOK if statement. Now return isSuccess.like this
@Override
protected Integer doInBackground(Long... params)
{
int isSuccess=0;
publishProgress(isOnline());
if(isOnline())
{
getDBData();
if(isOK)
{
isSuccess=1;
}
}
}
return isSuccess;
}
Now modify your PostExecute() to process the isSuccess value ruturned as result in onPostExecute
@Override
protected void onPostExecute(Integer result) {
if (ServerPD.isShowing())
{
ServerPD.dismiss();
}
if(result==1)
{
showResult();
}
}//End of onPostExecute
Hope this helps.
Instead of giving
private class DBTask extends AsyncTask<Long, Boolean, Integer>{
give
private class DBTask extends AsyncTask<Long, Boolean, Boolean>{
Then inside doInBackground() create a variable:
boolean success = false;
Inside ur ifOnline() make :
success = true;
And in the end of that method give:
return success;
Inside your onPostExecute() take parameter as boolean instead of Integer.
Give:
if(result) {
showResult();
}
Hope this may help you. onPostExecute() only gets called after the doInBackground().
The best way to attain your intention is to create a Handler
and display the Dialog box in that. some thing like this
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(
CardDetails.this);
alertbox.setMessage("Please Login First ...");
alertbox.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
});
alertbox.show();
}
}
and do this from your async task
new RefreshHandler().sendEmptyMessage(0)
Shijilal... When the doInBackground method ends you are notified in onPostExecute. The return value in doInBackground is sent to onPostExecute. You should not touch the UI in doInBackground which is running in a thread separate from the UI thread.
I would think to call isOnline before I called asynchTask.execute. I would think that the method in doInBackground would return type Result which would then be sent to onPostExecute. So consider: AsynchTask<SQLQuery,Void,ResultSet>
or whatever is appropriate.
精彩评论