Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, AsyncTask, check status?

Tags:

android

This is my code:

In onCreate:

 new LoadMusicInBackground().execute(); 

Then towards the end of my main class I have this code

/** Helper class to load all the music in the background. */ class LoadMusicInBackground extends AsyncTask<Void, String, Void> {     @Override     protected Void doInBackground(Void... unused) {          soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);         soundPoolMap = new HashMap<Integer, Integer>();          soundPoolMap.put(A1,                 soundPool.load(GameScreen_bugfix.this, R.raw.a, 1));         soundPoolMap.put(A3,                 soundPool.load(GameScreen_bugfix.this, R.raw.b, 1));         soundPoolMap.put(A5,                 soundPool.load(GameScreen_bugfix.this, R.raw.c_s, 1));         soundPoolMap.put(A6,                 soundPool.load(GameScreen_bugfix.this, R.raw.d, 1));         soundPoolMap.put(A8,                 soundPool.load(GameScreen_bugfix.this, R.raw.e, 1));         soundPoolMap.put(A10,                 soundPool.load(GameScreen_bugfix.this, R.raw.f_s, 1));         soundPoolMap.put(A12,                 soundPool.load(GameScreen_bugfix.this, R.raw.g_s, 1));         soundPoolMap.put(wrong,                 soundPool.load(GameScreen_bugfix.this, R.raw.wrong2, 1));          publishProgress("");         Log.v("SOUNDPOOL", "" + soundPoolMap);         return (null);     }      @Override     protected void onProgressUpdate(String... item) {         // text1.setText(item[0]);     }      @Override     protected void onPostExecute(Void unused) {         //Toast.makeText(GameScreen_bugfix.this, "music loaded!", Toast.LENGTH_SHORT).show();     } } 

If the music has not loaded I am getting a nullpointer exception, looking at the docs I see there is a getStatus() but I have tried something like this:

music_load_status=LoadMusicInBackground.getStatus() 

and that is not working :( How do I check if the background task is complete and the music has loaded?

Thanks!
Ryan

like image 977
Ryan Avatar asked Sep 28 '11 19:09

Ryan


People also ask

How to check if AsyncTask is finished Android?

Use getStatus() to get the status of your AsyncTask . If status is AsyncTask. Status. RUNNING then your task is running.

When AsyncTask is executed it goes through what steps?

An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .


2 Answers

getStatus() checks whether the the AsyncTask is pending, running, or finished.

LoadMusicInBackground lmib = new LoadMusicInBackground();  if(lmib.getStatus() == AsyncTask.Status.PENDING){     // My AsyncTask has not started yet }  if(lmib.getStatus() == AsyncTask.Status.RUNNING){     // My AsyncTask is currently doing work in doInBackground() }  if(lmib.getStatus() == AsyncTask.Status.FINISHED){     // My AsyncTask is done and onPostExecute was called } 

If you want to check if your action actually succeeded (i.e. the music was successfully loaded), then you need to come up with your own method that determines that. The getStatus() can only determine the state of the actual thread in AsyncTask.

like image 92
DeeV Avatar answered Sep 27 '22 22:09

DeeV


This is asynchronous programing - you should not check from UI thread, because this means that you are blocking the UI thread ( presumably running check in loop with Thread.sleep()?).

Instead you should be called when AsyncTask is done: from it's onPostExecute() call whatever method in Activity you need.

Caveat: the downside of this approach is that Activity must be active when background thread finishes. This is often not the case, for example if back is prwssed or orientation is changed. Better approach is to send a broadcast from onPostExecute() and then interested activities can register to receive it. Best part is that Activity only receives broadcast if it's active at that time, meaning that multiple Activities can register, but only the active one will receive it.

like image 33
Peter Knego Avatar answered Sep 28 '22 00:09

Peter Knego