Reputation: 1012
I write Android application with service and AsynTaskLoader, but it doen't work correctly because one variable (I store it in preferences) change value after onPostExetute will be completed(Loger show this), can I change it or create a delay?
My function in Service:
private void DisplayLoggingInfo() {
LocalLog.appendLog("Autobus66Service display logging info");
LocalLog.appendLog("Autobus66Service isLoaderRunning = "
+ MainActivity.isLoaderRunning);
if(MainActivity.isLoaderRunning==false){
new LoaderInBackground(getApplicationContext(),
Autobus66Service.this).execute((Void[]) null);
}
Preferences prefs = new Preferences(this);
boolean doActivityLoading = prefs.doActivityLoading();
LocalLog.appendLog("Autobus66Service doActivityLoading = "
+ doActivityLoading);
if (doActivityLoading) {
isRunning = MainActivity.isActive;
closeAnotherActivities();
LocalLog.appendLog("Autobus66Service isRunning = " + isRunning);
Log.d("Autobus66Service isMainActivityRunning"
+ isRunning,MainActivity.TAG6);
if (isRunning) {
MainActivity.isCallFromAnotherActivity = true;
sendBroadcast(intent);
} else {
startActvityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startActvityIntent);
}
Autobus66WakeLock.lockOn(this);
}
}
it uses variable doActivityLoading but in this example unforchunately it uses old value not new from LoaderInBackground. Can you say how to correct it?
Upvotes: 0
Views: 91
Reputation: 3692
if you want to do something after the onPostExecute, you should use an interface in your AsyncTask.
interface ASyncResponse {
public void onDone(boolean response);
}
then, define your AsyncTask and create a constructor that has the response object
public static class SomeTask extends AsyncTask<Void, Void, Boolean> {
ASyncResponse response;
// constructor
public SomeTask(ASyncResponse response) {
this.response = response;
}
@Override
protected Boolean doInBackground(Void... params) {
// do some BG work
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// send back the response once background process is done
response.onDone(result);
}
}
to use it, simply call
SomeTask someTask = new SomeTask(new ASyncResponse() {
@Override
public void onDone(boolean response) {
// once AsyncTask is done, do your stuff
}
});
someTask.execute();
Upvotes: 1