android AsyncTask保持挂起

时间:2016-02-25 23:45:13

标签: android android-asynctask

嗨,谢谢你的关注!,

有人可以帮助我?,我的应用程序保持progressdialog活动状态,而asynctask仍然在运行中。我尝试在执行后取消asynctask,但不起作用。

公共类MttoBitacoraFragment扩展了Fragment {

ListView  lv;
AutoCompleteTextView actEmpleado,actEstatus, actDepto;
List<Autcompletecls> listEmpelado=  new ArrayList<Autcompletecls>();
List<Autcompletecls> listEstatus=  new ArrayList<Autcompletecls>();
List<Autcompletecls> listDepto=  new ArrayList<Autcompletecls>();
AutocompleteAdapter adpEmpleado, adpEstatus, adpDepto;
ProgressDialog dialog;
WS webservice;

public MttoBitacoraFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_mtto_bitacora, container, false);
    String[][] para = new String[0][0];

    actEmpleado = (AutoCompleteTextView)view.findViewById(R.id.actMttoProblemaEmpleado);
    actEstatus =(AutoCompleteTextView)view.findViewById(R.id.actMttoProblemaEstatus);
    actDepto=(AutoCompleteTextView)view.findViewById(R.id.actMttoProblemaDepto);


    ansyWS call = new ansyWS("ConEmpleadoDLL", para);
    call.execute();
    call.cancel(true);
    while(call.getStatus() == AsyncTask.Status.RUNNING)
    {
        call.cancel(true);
    }

    if(call.getStatus() != null || call.getStatus() == AsyncTask.Status.RUNNING) {
        call.cancel(true);
        ansyWS call2 = new ansyWS("ConEstatusDLL",para);
        call2.execute();
        call2.cancel(true);
    }
    else {
        ansyWS call2 = new ansyWS("ConEstatusDLL",para);
        call2.execute();
        call2.cancel(true);
    } 


   actEmpleado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           Autcompletecls item = listEmpelado.get(position);
           Toast.makeText(getContext(), item.getId(), Toast.LENGTH_SHORT).show();
       }
   });

    actEstatus.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Autcompletecls item = listEstatus.get(position);
            Toast.makeText(getContext(), item.getId(), Toast.LENGTH_SHORT).show();
        }
    });
    return view;
}


class ansyWS extends AsyncTask<String,String,String>{
    private volatile boolean running = true;
    String[][] parametros;
    String methodname;
    SoapObject resultado;

    public ansyWS(String _methodname, String[][] _parametros)
    {
        methodname = _methodname;
        parametros = _parametros;
    }

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage(getResources().getString(R.string.dialog_message_save));
        dialog.setIndeterminate(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        webservice = new WS();
        resultado = webservice. LlamaWS(methodname,parametros);
        dialog.dismiss();
        if (resultado != null) {
            for (int i = 0; i < resultado.getPropertyCount(); i++) {
                SoapObject datos = (SoapObject) resultado.getProperty(i);
                switch (methodname) {
                    case "ConEmpleadoDLL":
                        listEmpelado.add(new Autcompletecls(datos.getProperty(0).toString(), datos.getProperty(1).toString()));
                        break;
                    case "ConEstatusDLL":
                        listEstatus.add(new Autcompletecls(datos.getProperty(0).toString(), datos.getProperty(1).toString()));
                        break;
                }

            }
            return methodname;
        } else {
            return "error";
        }
    }

    @Override
    protected void onPostExecute(String s) {
        while(running) {
            switch (s) {
                case "ConEmpleadoDLL":
                    actEmpleado.setThreshold(0);
                    adpEmpleado = new AutocompleteAdapter(getContext(), R.layout.activity_main, R.id.tv_autocomplete_text, listEmpelado);
                    actEmpleado.setAdapter(adpEmpleado);
                    break;
                case "ConEstatusDLL":
                    actEstatus.setThreshold(0);
                    adpEstatus = new AutocompleteAdapter(getContext(), R.layout.activity_main, R.id.tv_autocomplete_text, listEstatus);
                    actEstatus.setAdapter(adpEstatus);
                    break;
            }
        }
        cancel(true);
        dialog.dismiss();
    }

    @Override
    protected void onCancelled() {
        running = false;
        super.onCancelled();
    }
}


@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("SomeTag", System.currentTimeMillis() / 100L + "  onDestory()");
}

}

0 个答案:

没有答案