0

我需要制作在 AsyncTask 并发活动开始时创建的消失对话框。但是我尝试在 onPostExecute() 中插入 dialog.dismiss() 但它根本不显示对话框。

我正在获取 GPS 纬度和经度,当时我想显示 ProgressDialog。它会显示,但在活动之后我不能让它消失。

这是编码。

public class SearchActivity extends Activity {

    private ProgressDialog dialog;

    private static String PROVIDER = LocationManager.GPS_PROVIDER;
    private LocationManager locationManager;

    double longitude = 0.0;
    double latitude = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();

        if (extras == null) {
            Toast.makeText(this, "Something Wrong existing....",
                    Toast.LENGTH_SHORT).show();
            finish();
        }

        String utterance = extras.getString("utterance");
        String location = extras.getString("location");

        if (location == null) {
            dialog = ProgressDialog.show(this, "",
                    "Searching your location...", true);
            dialog.setCancelable(true);
            new GPSLocator().execute();

        }else{
            Toast.makeText(getApplicationContext(), "Location is from SpeechRecognizer Activity", Toast.LENGTH_LONG).show();
        }

    }

    public class GPSLocator extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            String info = getLocation();

            return info;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

                    dialog.dismiss();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }

        public String getLocation() {

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Location loc = locationManager.getLastKnownLocation(PROVIDER);

            if (loc == null) {
                return "GPS is not available. Location is null";
            } else {
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();

                return String.valueOf(latitude) + " - "
                        + String.valueOf(longitude);

            }

        }

    }

}
4

1 回答 1

2

您应该在 AsyncTask 类中的 onPreExecute () 中初始化对话框使用:

if (location == null) {
            dialog = ProgressDialog.show(this, "",
                    "Searching your location...", true);
            dialog.setCancelable(true);}

OnPreExecute 顾名思义在所有其他方法之前运行,所以

1)onPreExecute:对话框初始化

2)doinbackground:做你的事情和

3) onPostExecute 使用结果并关闭对话框。

参考:http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-08-13T17:36:34.890 回答