在許多人的幫助下,我得到了這樣的代碼:但即時通訊面臨兩個問題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);
}
}
PLease顯示你如何執行這個任務 – 2014-12-27 13:39:22
我從我的mainactivity調用這個:new ServicioInicial(MainActivity.this).execute(); – 2014-12-27 13:57:48