Android “进度”对话框不会关闭
我试图做的是在用户单击一个名为location_button的按钮后获取用户的位置,然后显示一个框,其中显示了有关其位置的一些信息。我试过这样做:Android “进度”对话框不会关闭,android,dialog,progress,dismiss,Android,Dialog,Progress,Dismiss,我试图做的是在用户单击一个名为location_button的按钮后获取用户的位置,然后显示一个框,其中显示了有关其位置的一些信息。我试过这样做: Button location_Button=(Button) findViewById(R.id.location_button); location_Button.setOnClickListener(new View.OnClickListener() { @Override public void on
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总是异步的,所以您必须使用线程来使用进度对话框。例如: 在代码中,当要启动对话框时,请调用异步类:
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).
}
}
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.os.AsyncTask;
公共类位置已更改扩展异步任务{
私有进程对话;
私人语境;
私有字符串消息;
公共位置已更改(上下文、字符串消息){
this.context=上下文;
this.message=消息;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=progressDialog.show(上下文,空,消息);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//在这里添加代码。
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
progressDialog.disclose();
//您可以添加在进度之后执行的代码(可能是结果)。
}
}
可以,但我不确定“final LocationManager LocationManager=(LocationManager)this.getSystemService(Context.LOCATION\u SERVICE)”的确切位置;geocoder=新的geocoder(这个,Locale.getDefault());'