2014-12-27 55 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