【问题标题】:progress dialog does not dismiss进度对话框不会关闭
【发布时间】:2013-07-05 03:24:06
【问题描述】:

我要做的是在用户单击一个名为 location_Button 的按钮后获取用户的位置,然后显示一个显示有关其位置的一些信息的框。我试过这样:

Button location_Button=(Button) findViewById(R.id.location_button);
    location_Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Define a listener that responds to location updates
           progressDialog = ProgressDialog.show(Settings.this, "", "Loading...");

            final LocationListener locationListener = new LocationListener() {

                public void onLocationChanged(Location location) {
                    // Called when a new location is found by the network location provider.

                    lat =location.getLatitude();
                    lon = location.getLongitude();
                    try{

                        addresses=(geocoder.getFromLocation(lat, lon, 1));
                        if (addresses.size() > 0) {
                            String city = addresses.get(0).getAddressLine(1);
                            String id = city.substring(0,city.length()-5);
                            String state1=String.valueOf(id.charAt(id.length()-3));
                            String state2=String.valueOf(id.charAt(id.length()-2));
                            String STATE = state1+state2;

                            locationManager.removeUpdates(this);
                            progressDialog.dismiss();

                            //alert dialog is established and displayed here



                        }
                        else {
                            //tv.setText("Oops...");
                        }

                    }catch(IOException e){
                        e.printStackTrace();
                    }

                }

                public void onStatusChanged(String provider, int status, Bundle extras) {}

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
            };

问题是进度对话框永远不会被关闭。按下按钮时没有进度对话框,它会搜索然后显示该框,而在进度对话框中,它会继续说正在加载并且永远不会被解雇。我的目标是在建立位置后关闭进度对话框,并且应该弹出警报对话框,但不是。进度对话框的位置不正确吗?

【问题讨论】:

    标签: android dialog progress dismiss


    【解决方案1】:

    正因为 Android 总是异步的,你必须使用线程来使用进度对话框。一个例子:

    在你的代码中,当你想启动对话框时,调用一个异步类:

    Button location_Button=(Button) findViewById(R.id.location_button);
        location_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LocationChanged locationChanged = new LocationChanged(MainActivity.this, "Loading...");
                locationChanged.execute();
            }
    

    现在您需要创建 LocationChanged.java:

    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.AsyncTask;
    
    public class LocationChanged extends AsyncTask<Void, Void, Void> {
    
        private ProgressDialog progressDialog;
        private Context context;
            private String message;
    
        public LocationChanged (Context context, String message) {
            this.context = context;
                    this.message = message;
            }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show (context, null, message);
        }
    
        @Override
        protected Void doInBackground( Void... params ) {
                // Here you add your code. 
    
                return null;
        }
    
        @Override
        protected void onPostExecute( Void result ) {
            super.onPostExecute (result);
    
            progressDialog.dismiss();
    
              // You can add code to be executed after the progress (may be a result).
        }
    
    }
    

    【讨论】:

    • 好的,但我不确定在哪里放置 'final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); geocoder = new Geocoder(this, Locale.getDefault());'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多