Two ways:
- Declare class extending AsyncTask as private class in parent Activity
- Pass Handler or Activity itself as param of class extending AsyncTask
If I were you, I'd follow the first option.
Look at DOCS:
class MyActivitySubclass extends Activity {
function runOnPostExecute(){
// whatever
}
private class MyTask extends AsyncTask<Void, Void, Void> {
void doInBackground(Void... params){
// do your background stuff
}
void onPostExecute(Void... result){
runOnPostExecute();
}
}
}
Note 1
Code placed in body of function onPostExecute
is already run on Activity thread, you should just mention that this
keywords leads to MyTask.this
and not MyActivitySubclass.this
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…