I want to create a progress dialog in android, keep it open for 2 seconds then close it.

Below is the code I have written:

```
package com.example.proressdialogtest;

import android.app.activity;
import android.app.ProgressDialog;
import android.os.bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState); 
        ProgressDialog pg = new ProgressDialog(MainActivity.this, 4);

        pg.show(MainActivity.this, null, "Searching...", false);

        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }  

        pg.dismiss();
}

```

When I run the code on my device, the ProgressDialog is opened and then it stays open, it does not close after 2 seconds. What am I doing wrong?

---

As per the answers below. I have added the onPreExecute() and onPostExecute() methods before and after the doInBackground method respectively.

Here is the code for the two methods.

```
ProgressDialog pd;

public void onPreExceute() {
    pd = new ProgressDialog(MainActivity.this);
    pd.show(MainActivity.this, "", "Searching...", false);
}

public void onPostExecute() {
    pd.dismiss();
}

```

The problem still persists. The progress bar will not close.