【问题标题】:Android ProgressDialog Annoying errorsAndroid ProgressDialog 恼人的错误
【发布时间】:2012-08-17 12:40:42
【问题描述】:

我正在构建一个使用 GPS 来确定用户当前国家/地区的应用程序。现在,由于 GPS 需要一段时间才能找到位置,所以在搜索时我想显示一个 ProgressDialog。

所以在我的 LocationListener 中,我调用了我之前声明的 ProgressDialog

pd

在它显示的部分的正下方,我创建了一个线程来执行 GPS 工作。由于某种原因,ProgressDialog 没有显示:/

这是我的代码:

pd = ProgressDialog.show(TipCalculatorActivity.this, "", "Looking for GPS satellites...");
        new Thread() 
        {
          public void run() 
          {
             try
               {
                Geocoder geocoder = new Geocoder(TipCalculatorActivity.this, Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = geocoder.getFromLocation(lat, lng, 1);
                    countryCode = addresses.get(0).getAddressLine(2);
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
                    finish();
                }
                if(countryCode==null){
                    Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
                    finish();
                }
                Toast.makeText(TipCalculatorActivity.this, countryCode, Toast.LENGTH_LONG).show();

              }
            catch (Exception e)
            {
                Log.e("tag",e.getMessage());
            }
        // dismiss the progressdialog   
          pd.dismiss();
         }
        }.start();

这里有什么问题?!

谢谢!

编辑

我的新代码: 我在哪里调用 AsyncTask:

    LocationListener onLocationChange=new LocationListener() {
    public void onLocationChanged(Location loc) {
        //sets and displays the lat/long when a location is provided
        lat = loc.getLatitude();
        lng = loc.getLongitude();

        MyAsyncTask task = new MyAsyncTask(TipCalculatorActivity.this, lng, lat);
        task.execute();
        Toast.makeText(TipCalculatorActivity.this, "Done :D", Toast.LENGTH_LONG).show();

    }

异步任务:

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

private Activity activity;
private ProgressDialog progressDialog;

private double longitude;
private double latitude;
private String countryCode;

public MyAsyncTask(Activity activity, double longitude, double latitude) {
    super();
    this.activity = activity;
    this.longitude = longitude;
    this.latitude = latitude;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    ProgressDialog.show(activity, "", "Looking for GPS satellites...");
}

@Override
protected Result doInBackground(Void... v) {
    Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
    List<Address> addresses;
    try {
        addresses = geocoder.getFromLocation(latitude, longitude, 1);
        countryCode = addresses.get(0).getAddressLine(2);
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
    }
    if(countryCode==null){
        Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
    }
    Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show();
    return null;
}

@Override
protected void onPostExecute(Result result) {
    progressDialog.dismiss();
    Toast.makeText(activity.getApplicationContext(), "Finished.", 
    Toast.LENGTH_LONG).show();
}
}

【问题讨论】:

  • 吐司是否立即出现?
  • 看看下面的答案

标签: android multithreading gps progressdialog


【解决方案1】:

如果我是你,我会为此使用 AsyncTask:

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

                        private Activity activity;
                        private ProgressDialog progressDialog;

                        private long longitude;
                        private long latitute;

            public MyAsyncTask(Activity activity, Long longitude, Long latitude) {
                            super();
                this.activity = activity;
                this.longitude = longitude;
                this.latitude = latitude;
            }

            @Override
            protected void onPreExecute() {
            super.onPreExecute();
                progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
            }

            @Override
            protected Result doInBackground(Void... v) {
            //do your stuff here
            return null;

            }

            @Override
            protected void onPostExecute(Result result) {
                progressDialog.dismiss();
                Toast.makeText(activity.getApplicationContext(), "Finished.", 
                        Toast.LENGTH_LONG).show();
            }


}

从活动中调用它:

MyAsyncTask task = new AsyncTask(myActivity.this, lng, lat);
task.execute();

【讨论】:

  • 我可以将像 lat lng 变量这样的数据传递给这个 AsyncTask 吗?
  • 当然可以!根据需要修改 AsynTask 的构造函数。我编辑了!!
  • 我可以发回数据吗?比如我的结果?
  • 是的,您可以使用 AsynTask.get() 方法。有关更多信息,请参阅此developer.android.com/reference/android/os/AsyncTask.html
  • 我不确定这是如何工作的,从来没有做过这样的事情。可以发个例子吗?
猜你喜欢
  • 2011-05-22
  • 2011-09-02
  • 2013-06-05
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多