Last active
September 25, 2024 03:30
-
-
Save cesarferreira/ef70baa8d64f9753b4da to your computer and use it in GitHub Desktop.
Advanced Android AsyncTask Callback example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cesarferreira.asynctaskcallback; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
SomeTask someTask = new SomeTask(getApplicationContext(), new OnEventListener<String>() { | |
@Override | |
public void onSuccess(String result) { | |
Toast.makeText(getApplicationContext(), "SUCCESS: "+result, Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onFailure(Exception e) { | |
Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
someTask.execute(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cesarferreira.asynctaskcallback; | |
/** | |
* Created by cesarferreira on 30/05/14. | |
*/ | |
public interface OnEventListener<T> { | |
public void onSuccess(T object); | |
public void onFailure(Exception e); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cesarferreira.asynctaskcallback; | |
import android.content.Context; | |
import android.os.AsyncTask; | |
/** | |
* Created by cesarferreira on 22/05/14. | |
*/ | |
public class SomeTask extends AsyncTask<Void, Void, String> { | |
private OnEventListener<String> mCallBack; | |
private Context mContext; | |
public Exception mException; | |
public SomeTask(Context context, OnEventListener callback) { | |
mCallBack = callback; | |
mContext = context; | |
} | |
@Override | |
protected String doInBackground(Void... params) { | |
try { | |
// todo try to do something dangerous | |
return "HELLO"; | |
} catch (Exception e) { | |
mException = e; | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
if (mCallBack != null) { | |
if (mException == null) { | |
mCallBack.onSuccess(result); | |
} else { | |
mCallBack.onFailure(mException); | |
} | |
} | |
} | |
} |
Thanks a lot!
Very Decent Approach.
how to unit test?
This SomeTask leaks context because there's no cancellation.
Clean
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
like!