2013-08-21 12 views
14

Ho due attività nella mia applicazione. Nella mia attività A sto usando un AsyncTask e la seconda attività B anche usando un altro AsyncTask. Nella mia attività A ho caricato alcuni dati sul server e nella mia attività B sto cercando di scaricare altri dati dal server. Entrambi questi sono in esecuzione in AsyncTask. Il mio problema è quando ho provato a scaricare i dati dal server nel metodo Attività B onPreExecute() ma il metodo doInBackground() non è stato chiamato è in attesa della prima attività di Attività A doInBackground() terminata. Perché è successo? E 'possibile eseguire più di un fondo azioni in un momento stesso ..È possibile eseguire più AsyncTask nello stesso tempo?

In Activity Un

ImageButton submit_button = (ImageButton) findViewById(R.id.submit_button); 

submit_button.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View record_button) 
     { 
         new Save_data().execute(); 
     } 
     }); 
class Save_data extends AsyncTask<String, Integer, Integer> 
{ 
    protected void onPreExecute() 
    { 

} 
    protected Integer doInBackground(String... arg0) 
{ 
    //uploading data here 
    } 

} 

nella mia attività di B

ImageButton get_button = (ImageButton) findViewById(R.id.get_button); 
    get_button.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View record_button) 
      { 
          new download_process().execute(); 
      } 
      }); 
    class download_process extends AsyncTask<String, integer, integer> 
    { 
     protected void onPreExecute() 
     { 
     Log.e("pre-execute","has been called");//This Log works well 
    } 
     protected Integer doInBackground(String... arg0) 
    { 
     //downloading data here 
     } 
    } 
+0

come utilizzare esecutore .. per favore mi dica .. – Amsheer

+1

possibile duplicato - (http://stackoverflow.com/questions/ [l'esecuzione di più AsyncTasks allo stesso tempo non è possibile?] 406 8984/running-multiple-asynctasks-at-the-time-not-possible) - che fornisce un'ottima spiegazione –

risposta

25

uso esecutore come segue

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    new Save_data().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location); 
} else { 
    new Save_data().execute(location); 
} 

Vedere this

+0

Qual è la posizione qui? – Amsheer

+0

grazie sta funzionando ... – Amsheer

+1

non riuscivo a capire la posizione ... potresti per favore elaborarlo ... –

15

Sì, lo è, ma dal momento che Honeycomb, c'è un cambiamento nel modo in cui AsyncTask viene gestito su Android. Da HC + vengono eseguiti sequenzialmente, invece di essere sparati in parallelo, come in passato. La soluzione è di chiamarli come questo (metterlo in classe strumenti separati o giù di lì):

public class AsyncTaskTools { 
    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) { 
     execute(task, (P[]) null); 
    } 

    @SuppressLint("NewApi") 
    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); 
     } else { 
      task.execute(params); 
     } 
    } 
} 

e allora si può semplicemente chiamare:

AsyncTaskTools.execute(new MyAsyncTask()); 

o con params (comunque io invece suggerisco params passign via compito costruttore):

AsyncTaskTools.execute(new MyAsyncTask(), <your params here>); 
+0

Ho ricevuto questo messaggio "Il metodo esegueAsyncTask (T, P []) non è definito per il tipo AsyncTasktools "Perché? – Amsheer

+0

corretto. Ma era un bug piuttosto semplice, perché non provavi a sistemarti? :) –

+0

Le classi interne non possono avere dichiarazioni statiche! –

Problemi correlati