在异步任务完成之前,Android进度情况不会显示

时间:2016-09-08 09:32:10

标签: android android-asynctask progressdialog

我有两个asynctask,第二个是第一个onPostExecute()的{​​{1}}。我希望显示进度对话框,直到两个asynctask完成。问题是我的asynctask只显示了几毫秒然后隐藏。我该如何解决这个问题?

progressdialog

2 个答案:

答案 0 :(得分:0)

我已将这两项任务移到单AsyncTask中。首先执行saveData(),然后执行saveImage()。看看:

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            showDialog();
        }

        @Override
        protected String doInBackground(String... params) {

            saveData();
            saveImage();
            // You are not using returned parameter in onPostExecute(). So, I am passing empty string
            return "";

        }

        @Override
        protected void onPostExecute(String result1) {

            super.onPostExecute(result1);

        }

               public void saveData()
        {
            SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata);
            cursor = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
                /*for(int i=0; i<cursor.getCount(); i++)
                {*/
            int k = 0;
            try
            {
                if (cursor.moveToFirst())
                {
                        /*while (cursor.moveToNext())*/
                    do
                    {
                        JSONArray arr = new JSONArray();
                        JSONObject jsonObject = new JSONObject();

                        jsonObject.put("ConsAcNo", cursor.getString(cursor.getColumnIndex("CONS_ACCOUNT_NO")));
                        jsonObject.put("MeterSrNo", cursor.getString(cursor.getColumnIndex("MTR_SERIAL_NO")));

                        //arr.put(jsonObject);

                        //request.properties.clear();
                        if (k == 0)
                        {
                            request.addProperty(parameter_getlistdata, jsonObject.toString());//add the parameters
                            request.addProperty(parameter_getlistdata1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters
                        }
                        else
                        {
                            request.setProperty(0, jsonObject.toString());//add the parameters
                            request.setProperty(1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters
                        }
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
                        envelope.setOutputSoapObject(request);
                        envelope.dotNet = true;
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
                        androidHttpTransport.call(soap_action_getlistdata, envelope);  // this is the actual part that will call the webservice
                        //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
                        SoapObject response = (SoapObject) envelope.bodyIn;

                        k++;
                    }
                    while (cursor.moveToNext());
                }
                cursor.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d("myError -", String.valueOf(e));
            }
            // }

            return resultSaveAllData;
        }

        public void saveImage()
        {
            SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);
            cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
                    /*for(int i=0; i<cursor.getCount(); i++)
                    {*/
            int k = 0;
            try
            {
                if (cursorImage.moveToFirst())
                {

                            /*while (cursor.moveToNext())*/
                    do
                    {
                        //request.properties.clear();

                        if (k == 0)
                        {
                            request.addProperty("fileByte1", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                            request.addProperty("fileName1", cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters

                        }
                        else
                        {
                            request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                            request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters

                        }
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
                        envelope.setOutputSoapObject(request);
                        envelope.dotNet = true;

                        HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
                        androidHttpTransport.call(soap_action_uploadImage, envelope);  // this is the actual part that will call the webservice
                        //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
                        SoapObject response = (SoapObject) envelope.bodyIn;

                        k++;

                    }
                    while (cursorImage.moveToNext());

                    db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N'");

                }
                cursorImage.close();


            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d("myError -", String.valueOf(e));
            }
            // }

            return resultSaveAllData;
        }
}

答案 1 :(得分:0)

将你的进度对话框放在第二个asynctask的OnpreExecute()中,并在postExceute中关闭这两个对话框:

//TO call web service save Data on Server
class async_SaveData extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        showDialog();
    }

    @Override
    protected String doInBackground(String... params) {

        SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata);

        cursor = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
        /*for(int i=0; i<cursor.getCount(); i++)
        {*/
        int k = 0;
        try {
            if (cursor.moveToFirst()) {
                /*while (cursor.moveToNext())*/
                do {

                    JSONArray arr = new JSONArray();
                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("ConsAcNo", cursor.getString(cursor.getColumnIndex("CONS_ACCOUNT_NO")));
                    jsonObject.put("MeterSrNo", cursor.getString(cursor.getColumnIndex("MTR_SERIAL_NO")));

                    //arr.put(jsonObject);

                    //request.properties.clear();
                    if (k == 0) {
                        request.addProperty(parameter_getlistdata, jsonObject.toString());//add the parameters
                        request.addProperty(parameter_getlistdata1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters
                    } else {
                        request.setProperty(0, jsonObject.toString());//add the parameters
                        request.setProperty(1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters
                    }
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
                    envelope.setOutputSoapObject(request);
                    envelope.dotNet = true;
                    try {
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
                        androidHttpTransport.call(soap_action_getlistdata, envelope);  // this is the actual part that will call the webservice
                        //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
                        SoapObject response = (SoapObject) envelope.bodyIn;

                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.d("myError -", String.valueOf(e));
                    }
                    k++;
                } while (cursor.moveToNext());
            }
            cursor.close();


        } catch (Exception e) {

        }
        // }

        return resultSaveAllData;

    }

    @Override
    protected void onPostExecute(String result1) {

        new async_SaveImage().execute();

        super.onPostExecute(result1);

    }
}






//TO call web service save Image on Server
    class async_SaveImage extends AsyncTask<String, Void, String> {

@Override
    protected void onPreExecute() {
        super.onPreExecute();

        showDialog();
    }


        @Override
        protected String doInBackground(String... params) {

            SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);

            cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
            /*for(int i=0; i<cursor.getCount(); i++)
            {*/
            int k = 0;
            try {
                if (cursorImage.moveToFirst()) {

                    /*while (cursor.moveToNext())*/
                    do {
                        //request.properties.clear();

                        if (k == 0) {
                            request.addProperty("fileByte1", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                            request.addProperty("fileName1", cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters

                        } else {
                            request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                            request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters

                        }
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
                        envelope.setOutputSoapObject(request);
                        envelope.dotNet = true;
                        try {
                            HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
                             androidHttpTransport.call(soap_action_uploadImage, envelope);  // this is the actual part that will call the webservice
                            //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
                            SoapObject response = (SoapObject) envelope.bodyIn;

                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.d("myError -", String.valueOf(e));
                        }
                        k++;

                    } while (cursorImage.moveToNext());

                    db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N'");

                }
                cursorImage.close();


            } catch (Exception e) {

            }
            // }

            return resultSaveAllData;

        }

  @Override
    protected void onPostExecute(String result1) {

        hideDialog();
        super.onPostExecute(result1);

    }
}