我正在构建一个使用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();
}
AsyncTask:
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();
}
}
答案 0 :(得分: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();