Supreet
Supreet

Reputation: 2541

how to show progress in android

i am new in android and i want to display progress when moving from one activity to other activity in android here is the code.Exception is "Activity com.example.mytest.ReadContactsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40ce4900 V.E..... R.....I. 0,0-295,62} that was originally added here"

class MyTask extends AsyncTask<Void, Integer, Void> {

Dialog dialog;
ProgressBar progressBar;
TextView tvLoading,tvPer;


@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog = new Dialog(ReadContactsActivity.this);
    dialog.setCancelable(false);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.progressdialog);

    progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
    tvLoading = (TextView) dialog.findViewById(R.id.tv1);
    dialog.show();
}

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

 textViewDisplay = (TextView) findViewById(R.id.textViewDisplay);
 ContentResolver cr = getContentResolver();
 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
        null, null, null, null);

 if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            System.out.println("name : " + name + ", ID : " + id);
            textViewDisplay.append("Name: ");
            textViewDisplay.append(name);

            // get the phone number
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                   new String[]{id}, null);
            while (pCur.moveToNext()) {
                  String phone = pCur.getString(
                         pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  textViewDisplay.append(",Number: "+ phone);
                  textViewDisplay.append("\n");      
            }
            pCur.close();
            }
        }
    }
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    progressBar.setProgress(values[0]);
    tvLoading.setText("Loading...  " + values[0] + " %");
    tvPer.setText(values[0]+" %");
}

@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    dialog.dismiss();

    AlertDialog alert = new AlertDialog.Builder(ReadContactsActivity.this)
            .create();

    alert.setTitle("Completed!!!");
    alert.setMessage("Your Task is Completed SuccessFully!!!");
    alert.setButton("Dismiss", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    });
    alert.show();
}
}

Upvotes: 0

Views: 291

Answers (3)

Pragnani
Pragnani

Reputation: 20155

doInBackground not run in the UI thread but you are trying to get the view from doInBackground

textViewDisplay = (TextView) findViewById(R.id.textViewDisplay); //wrong

remove this and place it onPreExecute method

And you can really do UI operation on non UI Thread , so don't do any operatons on textViewDisplay in doinBackGround.

Finally Append your data to the string and return in doInBackground and you'll get that value in onPostExecute as a parameter use that to set Textvalue something like this

public String doInBackGround()
{
--
return result;
}
public void onPostExecute(String result)
{
textViewDisplay.setText(result);
}

Upvotes: 3

Geobits
Geobits

Reputation: 22342

There are a couple things wrong here. I can't say for sure what's causing the leak, but before you chase that down, fix these issues:

  • You really shouldn't be messing with textViewDisplay in the background thread. If you need/want to update your UI while it's working, use publishProgress() to do that.

  • I don't see anywhere that you call publishProgress() at all right now. That means that your onProgressUpdate() method is likely never being called.

Upvotes: 1

seymatanoglu
seymatanoglu

Reputation: 151

I don't see any issues on your code, but progressdialog may be null or etc.

I have written show & hide functions and using them. I call showLoading() function before excuting asynctask (in activity - actually they are in my baseactivity) and hideLoading() in onPostExecute.

Have a try please. Hope it helps.

Regards.

protected void showLoading() {
    if (dialog == null) {
        dialog = ProgressDialog.show(this, "", this.getResources()
                .getString(R.string.loading));
        dialog.setMessage(this.getResources().getString(R.string.loading));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                 if(dialog.isShowing())
                     dialog.dismiss();
                 finish();
            }
        });
        dialog.show();
    }
}

protected void hideLoading() {
    if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
        dialog = null;
    }
} 

Upvotes: 1

Related Questions