try this: the first call cancel method on the instance of AsyncTask
findViewById(R.id.cancelButton).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mTask.cancel(true);
}
});
Then you need to add check if your async task has been canceled:
@Override
protected String doInBackground(String... params) {
try{
// File's binary content
java.io.File fileContent = new java.io.File(localFilePath);
final FileInputStream fileStream = new FileInputStream(fileContent);
InputStreamContent mediaContent = new InputStreamContent(FileInfo.getMime(localFilePath), new BufferedInputStream(fileStream));
....
uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
void progressChanged(MediaHttpUploader uploader) {
if(isCancelled()) {
// could not find another way to cancel transferring except that:
fileStream.close();
}
else {
mediaHttpUploaderProgressListener(uploader);
}
}
});
....
} catch (IOException e) {
Log.e(TAG,"IOEXCEPTION Message:\n"+e.getMessage());
}
return null;
}
Hope this will work.
Have to add following - this is not a 'right' way how to implement transferring operations. Keep in mind that your activity may be stopped and even destroyed and than created again while you task is working. Event worse thing may happen. Running AsyncTask cannot prevent destroying the whole app when system runs out of memory while transferring is still going on if your app does not have any activity in foreground. In this case transferring may even not being completed.
It might be better to consider moving transferring operations to a dedicated service component.