我需要制作在 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);
}
}
}
}