2014-12-27 59 views
0

在許多人的幫助下,我得到了這樣的代碼:但即時通訊面臨兩個問題1 - 看起來線程睡眠隻影響線程而不是消息進行中的對話框,因此Tarea finalizada不能被看作出現並迅速消失。如果我設置Tarea inicializada(開始任務)的消息,它不能顯示...如果我把它放在後臺它不顯示,如果我把它放在「Cargando datos por favor espere」之後(加載數據請稍候)它顯示Tarea inicializada但不是Cargando數據。我需要一些技巧來查看不同的消息...Asynk任務不執行doinBackground

理想是: - 「Cargando DATOS POR青睞espere」(兩秒) - 利亞inicializada(兩秒) - 利亞finalizada(兩秒鐘)。 - 辭退,並返回到主活動

再次感謝

類ServicioInicial擴展的AsyncTask <虛空,虛空,虛空> { 私人語境mContext;

ProgressDialog mProgress; 
private ServicioInicialFinalizado mCallback; 

public ServicioInicial(Context context) { 
    this.mContext=context; 
    this.mCallback= (ServicioInicialFinalizado) context; 
} 


@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    mProgress= new ProgressDialog(mContext); 
    mProgress.setMessage("Cargando contactos...Por favor espere"); 
    mProgress.show(); 
    // mProgress.setMessage ("Tarea comenzada"); 

} 


@Override 
protected Void doInBackground(Void... resultado) { 


    try 
    { 
     //Aqui simulo un calculo supercomplicado y lo ralentizamos a 500 ms por numero 


      // mProgress.setMessage("Tarea comenzada"); 

      //Reseteo la agendaGlobal 
      AgendaGlobal.getInstance().miAgenda.clear(); 

      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
      AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
     // resultado[] = (Integer)(Math.random()*(3))+1;// numero = (int) (Math.random() *6) + 1; para un dado 

      // mProgress.setMessage("Resultado Int:" + resultado); 

      // resultado = (Integer) resultado; // convierto a entero me aseguro 
      Thread.sleep(3000); 

      //mProgress.setMessage ("Tarea finalizada"); 

      //SystemClock.sleep(1000); 


    } catch (Exception e) { 
     e.printStackTrace(); 
     } 

    // mProgress.setMessage ("Tarea finalizada"); 
    return null; 
    } 


@Override 
protected void onCancelled(Void result) { 
    // TODO Auto-generated method stub 
    super.onCancelled(null); 
} 


@Override 
protected void onPostExecute(Void result) { 

    mProgress.setMessage ("Tarea finalizada"); 

    try { 
     Thread.sleep (3000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    mProgress.dismiss(); 
    /*if(mProgress != null){ 
     mProgress.dismiss(); 
     }*/ 
    //Aqui es donde devolvemos los datos a donde nos llama 
    mCallback.onAcabeInicializacion(5); 
    super.onPostExecute(null); 
} 


@Override 
protected void onProgressUpdate(Void... values) { 
    // TODO Auto-generated method stub 
//mProgress.setMessage(values[0]); 
    //super.onProgressUpdate(values); 
} 

}

+0

PLease顯示你如何執行這個任務 – 2014-12-27 13:39:22

+0

我從我的mainactivity調用這個:new ServicioInicial(MainActivity.this).execute(); – 2014-12-27 13:57:48

回答

0

您需要延長AsyncTask <Void, String, Void>和使用OnProgressUpdate更新的消息。

讓你的班上這些變化:

class ServicioInicial extends AsyncTask <Void, String, Void> { 

private Context mContext; 

ProgressDialog mProgress; 
private ServicioInicialFinalizado mCallback; 

public ServicioInicial(Context context) { 
this.mContext=context; 
this.mCallback= (ServicioInicialFinalizado) context; 
} 


@Override 
protected void onPreExecute() { 
super.onPreExecute(); 
mProgress= new ProgressDialog(mContext); 
mProgress.setMessage("Cargando contactos...Por favor espere"); 
mProgress.show(); 
} 


@Override 
protected Void doInBackground(Void... resultado) { 

try 
{ 
    Thread.sleep(2000); 

     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 

} catch (Exception e) { 
    e.printStackTrace(); 
} 

    publishProgress("Tarea inicializada"); 
    try { 
     Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
    } 

publishProgress("Tarea finalizada"); 
    try { 
     Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
    } 

return null; 
} 

@Override 
protected void onPostExecute(Void result) { 

if(mProgress != null){ 
mProgress.dismiss 
} 

mCallback.onAcabeInicializacion(5); 
super.onPostExecute(result); 
} 


@Override 
protected void onProgressUpdate(String... values) { 
mProgress.setMessage(values[0]); 
super.onProgressUpdate(values); 
} 
} 

,然後再次運行你的應用程序。

編輯:

要清除ArrayList添加到您的活動:

@Override 
public void onStop() { 
    super.onStop(); 
    miAgenda.clear(); //Clear ArrayList 
} 
+0

謝謝...現在它似乎doinBackground ...我加載聯繫..但現在我不能設置開始任務和完成任務的消息。現在它停留在「Cargando datos」的信息中,並且不會說任何其他內容或拒絕回到主要活動.... – 2014-12-27 13:47:56

+0

您只能在OnPreExecute和OnPostExecute中設置消息。 – max59 2014-12-27 13:54:35

+0

現在我已經做到了......我對你告訴我的setmessage發表了評論......然後在加載聯繫人(它現在做了什麼)之後,它應該在post執行和setmessage中輸入並解僱......但是doesnt .. 。 – 2014-12-27 14:01:50

1

當你在doInBackgroundMethod你是從UI線程工作的一個主題分開,以便你可以」只是做任何事情來更新你的視覺界面。所以,在doInBackground上沒有辦法改變它。

相反,您可以使用OnPreExecute和OnPostExecute來處理您的進度。對於什麼,我想你想它會是這樣的:

private Context mContext; 

ProgressDialog mProgress; 
private ServicioInicialFinalizado mCallback; 

public ServicioInicial(Context context) { 
    this.mContext=context; 
    this.mCallback= (ServicioInicialFinalizado) context; 
} 


@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    mProgress= new ProgressDialog(mContext); 
    mProgress.setMessage("Cargando contactos...Por favor espere"); 
mProgress.show(); 

mProgress.setMessage("Tarea comenzada"); 

} 




@Override 
protected Void doInBackground(Void... resultado) { 


    try 
    { 
     //Aqui simulo un calculo supercomplicado y lo ralentizamos a 500 ms por numero 




     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Belen", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.aguila)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Daniel", "c/ Diego Madrazo","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.caballo)); 
     AgendaGlobal.getInstance().miAgenda.add(new contactoAgenda("Eduardo", "c/ Segovia","92458", "[email protected]",true, true, false, false, true,"Familia", R.drawable.camaleon)); 
    // resultado[] = (Integer)(Math.random()*(3))+1;// numero = (int) (Math.random() *6) + 1; para un dado 

     // mProgress.setMessage("Resultado Int:" + resultado); 

     // resultado = (Integer) resultado; // convierto a entero me aseguro 
     Thread.sleep(200); 

     //mProgress.setMessage ("Tarea finalizada"); 

     SystemClock.sleep(1000); 


} catch (Exception e) { 
    e.printStackTrace(); 
    } 

// mProgress.setMessage ("Tarea finalizada"); 
return null; 
} 



@Override 
protected void onCancelled(Void result) { 
    // TODO Auto-generated method stub 
    super.onCancelled(null); 
} 


protected void onPostExecute() { 

    mProgress.setMessage ("Result Integer en postexecute:"); 
    mProgress.setMessage ("Tarea terminada"); 
    try { 

} catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
mProgress.dismiss(); 

//Aqui es donde devolvemos los datos a donde nos llama 
mCallback.onAcabeInicializacion(5); 
super.onPostExecute(null); 
} 


@Override 
protected void onProgressUpdate(Void... values) { 
    // TODO Auto-generated method stub 
// mProgress.setMessage(values[0]); 
    //super.onProgressUpdate(values); 
} 

如果mProgress沒有在方法onPostExecute更新,那是因爲你的doInBackground沒有完成做的工作,所以來看看這點,可以與你的睡眠者。

+1

調用'Thread.sleep() ''OnPostExecute'只會阻止UI,這會導致ANR(應用程序未響應)。 – max59 2014-12-27 14:58:15

+0

確實沒有意識到 – Buendiadas 2014-12-27 15:02:42