2011-09-03 29 views
16

Sto usando i compiti asincroni per ottenere una stringa dall'attività del menu e caricare alcune cose ... ma io sono non in grado di farlo .. Io lo uso nel modo giusto e sto passando il parametri correttamente? Vedi lo snippet di codice. graziePassare i parametri a Asynctask

private class Setup extends AsyncTask<Void, Integer, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      if (!(getIntent().getExtras().isEmpty())) { 
       Bundle gotid = getIntent().getExtras(); 
       identifier = gotid.getString("key"); 
      } 
     } catch (Exception e) { 
      e.getStackTrace(); 
     } finally { 

      if (identifier.matches("abc")) { 
       publishProgress(0); 
       db.insert_fri(); 
      } else if ((identifier.matches("xyz"))) { 
       publishProgress(1); 
       db.insert_met(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... i) { 
     // start the song here 
     if (i[0] == 0) { 
      song.setLooping(true); 
      song.start(); 
     } 
    } 

    @Override 
    protected void onPostExecute(Void res) { 

    } 

    @Override 
    protected void onPreExecute() { 
     // do something before execution 
    } 
} 

risposta

15

Invece di questo vorrei fare

private class Setup extends AsyncTask<String, Integer, Void> { 

    @Override 
    protected Void doInBackground(String... params) { 
    String identifier = params[0]; 

      if (identifier.matches("abc")) { 
       publishProgress(0); 
       db.insert_fri(); 
      } else if ((identifier.matches("xyz"))) { 
       publishProgress(1); 
       db.insert_met(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... i) { 
     // start the song here 
     if (i[0] == 0) { 
      song.setLooping(true); 
      song.start(); 
     } 
    } 

    @Override 
    protected void onPostExecute(Void res) { 

    } 

    @Override 
    protected void onPreExecute() { 
     // do something before execution 
    } 
} 

e verificare la presenza di "identificatore" prima di richiamare l'AsyncTask per evitare sovraccarico di creare un AsyncTask

come questo

if (!(getIntent().getExtras().isEmpty())) { 
       Bundle gotid = getIntent().getExtras(); 
       identifier = gotid.getString("key"); 
       new Setup().execute(identifier); 
    } 
4

un modo semplice è quello di aggiungere un costruttore:

public Setup(String a, Int b) { 
    this.a = a; 
    this.b = b; 
} 
0

AsyncTask indica che doInBackground() restituisce Void, onProgressUpdate() prende i parametri Integer e doInbackground richiede ... Param di stringa!

Quindi non hanno bisogno (e non dovrebbe) utilizzare intenti, dal momento che è destinato ad essere utilizzato per il passaggio di argomenti attraverso attività, non Threads.

E come detto prima, è possibile effettuare un costruttore e un parametro globale per la classe denominata "identificativo"

public class Setup... 
{ 
    private String identifier; 

    public Setup(String a) { 
    identifier = a; 
    } 
} 

sperato che potrebbe aiutare. Saluti

23

evitare di aggiungere un costruttore.

semplicemente passare i vostri paramters nel compito di eseguire il metodo

new BackgroundTask().execute(a, b, c); // can have any number of params 

Ora la classe sfondo dovrebbe assomigliare a questo

public class BackgroundTask extends AsyncTask<String, Integer, Long> { 

    @Override 
    protected Long doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     String a = arg0[0]; 
     String b = arg0[1]; 
     String c = arg0[2]; 
     //Do the heavy task with a,b,c 
     return null; 
    } 
    //you can keep other methods as well postExecute , preExecute, etc 

} 
+4

"evitare di aggiungere un costruttore" perché? Sto cercando di determinare il motivo per cui utilizzare un costruttore per avviare campi privati ​​in un asynctask è una cosa brutta in Android. – bsautner

+0

non ho detto che sia una cattiva idea, ma solo per il gusto di passare params non passare attraverso il percorso costruttore come si finirà per sprecare memoria. – HimalayanCoder

+1

Quanto memoria potrebbe sprecare? –

Problemi correlati