显示ProgressDialog失败

时间:2018-07-26 11:45:15

标签: android android-asynctask progressdialog

我知道对此有很多疑问,但我感到绝望... 我试图在执行AsyncTask时显示一个对话框,这很容易,但是我却做不到...

我尝试过使用.execute().get(),并且提出了for的要求,以使该任务花费很长时间,但是什么也没有。

这是我的课程:

public class OrdenNuevaActivity extends Activity {


@Override
protected void onCreate(Bundle bundle) {
    Log.v("FLUJO", this.getClass().toString());
    super.onCreate(bundle);
    setContentView(R.layout.activity_orden_nueva);
    aceptarOrdenNuevaButton = (Button) findViewById(R.id.aceptarOrdenNuevaButton);

    aceptarOrdenNuevaButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ConnectivityManager connMgr = (ConnectivityManager) 
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                        TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase tareaCambiarEstadoOrdenEnBDServer = new TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase(idOrden, Estados.ACEPTADO,OrdenNuevaActivity.this);
                try {

                    tareaCambiarEstadoOrdenEnBDServer.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
                Toast toast1 = Toast.makeText(getApplicationContext(), "Orden Aceptada", Toast.LENGTH_SHORT);
                toast1.show();
                NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notifManager.cancelAll();
                UtilsVictor.addPuebloAlAceptarOrden(context,terminoOrdenAceptadaDetalleTV.getText().toString());
                finish();



            } else {
                AlertDialog alertDialog = new AlertDialog.Builder(OrdenNuevaActivity.this).create();
                alertDialog.setTitle("ERROR!");
                alertDialog.setMessage("NO hay cobertura, vuelva a intentarlo mas tarde");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                alertDialog.setIcon(R.drawable.common_signin_btn_icon_normal_light);
                alertDialog.show();
            }
        }
    });


}


public class TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase extends AsyncTask<String,Integer,Boolean>{

    private static final String TAG = "AppMovil";
    private Integer mOrdenId;
    private String mEstadoString;
    boolean resul = true;
    boolean tareaRealizada = false;
    boolean catchPasado = false;
     private Context mContext;
     private ProgressDialog mPD;



    public TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoStringConSpinnerDentroDeLaClase(Integer idO ,String estadoO, Context cntx) {
        mContext = cntx;
        mOrdenId = idO;
        mEstadoString = estadoO;
        mPD = new ProgressDialog(mContext);
    }
    public boolean getResultado(){
        return resul;
    }
    public boolean getTareaRealizada(){
        return tareaRealizada;
    }
    public boolean getCatchPasado(){
        return catchPasado;
    }

    @Override
    protected void onPreExecute() {

        mPD.setTitle("Please Wait..");
        mPD.setMessage("Loading...");
        mPD.setCancelable(false);
        mPD.show();


    }
    @Override
    protected Boolean doInBackground(String... params) {
         for(int i=0; i<999999;i++){
                Log.v("A", "A");
            }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", mOrdenId);
            jsonObject.put("estado", mEstadoString);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        HttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(URIS.URI_TareaCambiarEstadoOrdenEnBDServerPorIdOrdenYEstadoString);
        post.setHeader("content-type", "application/json");

        try {
            StringEntity stringEntity = new StringEntity(jsonObject.toString());
            post.setEntity(stringEntity);

            HttpResponse response = httpClient.execute(post);
            int responseCode = response.getStatusLine().getStatusCode();
            if(responseCode >200 & responseCode < 400) {
                Log.v(TAG, "Orden Enviada OK a nuestro Server");
                tareaRealizada = true;
                resul = true;
            } else {
                Log.v(TAG, "FALLO al Enviar Orden a nuestro Server");
                resul = false;
                tareaRealizada = true;
            }

        } catch (Exception e) {
            catchPasado = true;
            resul = false;
            e.printStackTrace();
        } 
        return resul;
    }
    @Override
    protected void onCancelled() {
        if(mPD.isShowing()){ 
            mPD.dismiss();
        }
    }
    @Override
    protected void onPostExecute(Boolean result) {
        if(mPD.isShowing()){ 
            mPD.dismiss();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }


    }

}

哪里出了错?

0 个答案:

没有答案