如何在从SQLite同步数据时在AsyncTask中显示ProgressDialog?

时间:2016-09-10 05:51:19

标签: android sqlite android-asynctask progressdialog

我想将所有sqlite数据同步到服务器。一切都像一个魅力,但我的ProgressDialog没有显示,直到所有数据同步到服务器。当我按下同步按钮时,所有数据都成功保存到服务器,但ProgressDialog未显示,直到所有数据同步。

//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' and ProjectId = '" + gp.number + "'", 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")));
                        jsonObject.put("KNoNumber", cursor.getString(cursor.getColumnIndex("KNO_NO")));

                        //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();
        }

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

            SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);

            cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N' and ProjectId = '" + gp.number + "'", 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

                            request.addProperty("fileByte2", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters
                            request.addProperty("fileName2", cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters

                            request.addProperty("fileByte3", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters
                            request.addProperty("fileName3", cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters

                            request.addProperty("fileByte4", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters
                            request.addProperty("fileName4", cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters

                            request.addProperty("fileByte5", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters
                            request.addProperty("fileName5", cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters

                            request.addProperty("fileByte6", cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters
                            request.addProperty("fileName6", cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//add the parameters
                            // request.addProperty("fileName", "123.jpeg");//add the parametershe 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

                            request.setProperty(2, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters
                            request.setProperty(3, cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters

                            request.setProperty(4, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters
                            request.setProperty(5, cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters

                            request.setProperty(6, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters
                            request.setProperty(7, cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters

                            request.setProperty(8, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters
                            request.setProperty(9, cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters

                            request.setProperty(10, cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters
                            request.setProperty(11, cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//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());


                }
                cursorImage.close();


            } catch (Exception e) {

            }
            // }

            return resultSaveAllData;

        }

        @Override
        protected void onPostExecute(String result1) {
            //db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N' and ProjectId = '"+gp.number+"'");
            hideDialog();
            Toast.makeText(DashBoardActivity.this, "Records synced with server", Toast.LENGTH_LONG).show();
            super.onPostExecute(result1);

        }
    }

    // this is used to show diologue
    private static void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    // this is used to hide diologue
    public static void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

1 个答案:

答案 0 :(得分:0)

使用Thread代替AsyncTask。如果发现AsyncTask可能会导致UI卡住。

即,例如:

new Thread(new Runnable() {
@Override
    public void run() {
int i = 0;
        while (i < 1000) {
i++;
//your loop code

//to touch UI thread use this:

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
//for example,
textview.setText("MyText");
            }
        });

//sleep if you need:

try {
                            Thread.sleep(100L);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
}

    }
}).start();

或试试这个:

 @Override
    protected Void doInBackground(String... urls) {
      try {
        int i = 0;
        while ( i < 10) {
         publishProgress(++i);
        }
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return null;
    }

//to update UI
 @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      tvInfo.setText("TICK" + values[0]);
    }

<强> 修改

private static void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    // this is used to hide diologue
    public static void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

    public void start_all() {


new Thread(new Runnable() {
    @Override
    public void run() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showDialog();
            }
        });

        SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata);

            cursor = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N' and ProjectId = '" + gp.number + "'", 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")));
                        jsonObject.put("KNoNumber", cursor.getString(cursor.getColumnIndex("KNO_NO")));

                        //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) {

            }


            SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);

            cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N' and ProjectId = '" + gp.number + "'", 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

                            request.addProperty("fileByte2", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters
                            request.addProperty("fileName2", cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters

                            request.addProperty("fileByte3", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters
                            request.addProperty("fileName3", cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters

                            request.addProperty("fileByte4", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters
                            request.addProperty("fileName4", cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters

                            request.addProperty("fileByte5", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters
                            request.addProperty("fileName5", cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters

                            request.addProperty("fileByte6", cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters
                            request.addProperty("fileName6", cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//add the parameters
                            // request.addProperty("fileName", "123.jpeg");//add the parametershe 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

                            request.setProperty(2, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters
                            request.setProperty(3, cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters

                            request.setProperty(4, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters
                            request.setProperty(5, cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters

                            request.setProperty(6, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters
                            request.setProperty(7, cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters

                            request.setProperty(8, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters
                            request.setProperty(9, cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters

                            request.setProperty(10, cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters
                            request.setProperty(11, cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//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());


                }
                cursorImage.close();


            } catch (Exception e) {

            }


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 hideDialog();
                Toast.makeText(DashBoardActivity.this, "Records synced with server", Toast.LENGTH_LONG).show();

            }
        });



    }
}).start();


    }