Tuesday, 12 December 2017

How can i cancel the AsynsTask in Android?

Hi onCancel of dialog i want to cancel to server call but
i m facing problem that even i cancel the task, it hits my server and modifies the data.
How can I resolve this issue ? Below is my
code..







private class UserBoardingTask extends AsyncTask {
@Override

protected void onPreExecute() {
progressDialog = new
ProgressDialog(getActivity());

progressDialog.setMessage(getResources().getString(R.string.please_wait));

progressDialog.setIndeterminate(false);

progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new
OnCancelListener() {
@Override

public void
onCancel(DialogInterface dialog) {
if (userOnBoardingTask!= null &&
userOnBoardingTask.getStatus() != AsyncTask.Status.FINISHED &&
!userOnBoardingTask.isCancelled()) {

userOnBoardingTask.cancel(true);

}
}

});
progressDialog.show();
}



@Override
protected Void doInBackground(String... urls) {
String
boardingURL=null;


boardingURL=
getUrl();


UserOnBoardingDTO userOnBoardingDetailsDTO =
AppStateManager.getUserBoardingDetails();

try{


RestAPIManager.putToNSWebService(boardingURL,
userOnBoardingDetailsDTO, username, password);

}



catch (Exception e) {

errorMessage=getResources().getString(R.string.unknown_exp);
}

return null;

}

@Override

protected void onCancelled() {
super.onCancelled();

closeProgressDialog();
errorMessage="";

AppStateManager.setUserBoardingDetails(null);

userOnBoardingTask=null;


}


@Override
protected void onPostExecute(Void res) {

closeProgressDialog();
userOnBoardingTask=null;

if(!FieldsValidator.isBlank(errorMessage)){

CommonUtil.showToast(getActivity(),errorMessage);
errorMessage="";

return;

}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...