Elshan
Elshan

Reputation: 7693

Handle long time network oparation with android

I am new to android development.I have done some android application to cummounicate with webservice and get data from it into local database in android device.I used AsyncTask<,,> Method to transfer data from internet.

Then I used ProgressDialog to indecate the data transfering.What i am doing.checking how meny tables have to sync and getting all data with for loop and through the for loop call my AsyncTask().execute() Method. (code shows bellow)

Issue is when showing the progress dialog loop length is grater than 1 open several progress dialogs on top itselft and they are not close.But already i called close event

DataTransfering Method

private class NetworkTransfer extends AsyncTask<DataLocation, Void, Void> {    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LocationFinder.this); // show ProgressDialog
        pDialog.setMessage("Processing...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(DataLocation... arg0) {
        NetworkUtil networkUtil = new NetworkUtil(); //my http connection class
        DataLocation loc = arg0[0];
        networkUtil.InsertDataEmp(loc.getC_device_modle(),
                loc.getC_usercd(), loc.getC_brach()); 

        DataSource dsx = new DataSource(getApplicationContext());
        dsx.updateLocDt(loc.getC_brach()); // send data to webserver
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())        
            pDialog.dismiss(); // for dissmiss the ProgressDialog

    }

Function to run execure() method in Button Click Event shows bellow.

public void sendAllUnSyncData() {
        DataSource ds = new DataSource(getApplicationContext());
        final List<DataLocation> data = ds.GetLocList();
        for (int i = 0; i < data.size(); i++) {
            final NetworkTransfer networkObject = new NetworkTransfer();
            networkObject .execute(data.get(i)); // call AsyncTask Method
        }       
    }

When Running this code if loop length is bigger than (i>1)one (1) Progress Dioalog not closed.But if it's equals to one (1) , (i==1)it's worked!

Also I was tryied with Thread,but result was same.

Upvotes: 0

Views: 45

Answers (1)

Damien R.
Damien R.

Reputation: 3373

In your onPreExecute, try to add this:

    protected void onPreExecute() {
            super.onPreExecute();
    if (pDialog == null || !pDialog.isShowing()){
            pDialog = new ProgressDialog(LocationFinder.this); // show ProgressDialog
            pDialog.setMessage("Processing...");
            pDialog.setCancelable(false);
            pDialog.show();
     }
}

Then set 2 global variables: int dataSized and int dataDone=0. Initiate dataSized in your sendAllUnSyncData like this:

public void sendAllUnSyncData() {
        DataSource ds = new DataSource(getApplicationContext());
        final List<DataLocation> data = ds.GetLocList();
dataSized=data.size();
        for (int i = 0; i < data.size(); i++) {
            final NetworkTransfer networkObject = new NetworkTransfer();
            networkObject .execute(data.get(i)); // call AsyncTask Method
        }       
    }

Then on your onPostExecute, do this:

@Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
dataDone++;
if(dataDone==dataSized){
        if (pDialog.isShowing())        
            pDialog.dismiss(); // for dissmiss the ProgressDialog
     }
}

Let me know if it's working.

Upvotes: 1

Related Questions