我檢查活動何時啓動位置服務是否打開,如果沒有打開啓動「啓用位置活動」意圖的對話框。一旦我從它返回,我正在檢查該位置是否真的被啓用,如果是的話,我正在關閉警報對話框。無法在onResume()後關閉AlertDialog()
理論上這應該可行,但是當我的活動恢復時,並且打電話給dialog.dismiss()
絕對沒有任何反應。
我的代碼如下 - :
public class LocationUtils {
private static AlertDialog dialog_ = null;
public static void checkAndEnableLocationServices(final Activity context) {
LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("gps_enabled = " + gps_enabled);
System.out.println("network_enabled = " + network_enabled);
if (!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Location services are disabled");
builder.setPositiveButton("Enable Location Services", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(myIntent);
//get gps
}
});
builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
context.finish();
}
});
//For future reference.
AlertDialog dialog = builder.create();
dialog_ = dialog;
dialog.show();
} else {
if(dialog_!=null) {
dialog_.dismiss();
}
}
}
}
在我的主要活動我有一個回調的onResume,做以下各項:
@Override
protected void onResume() {
super.onResume();
System.out.println("Activity resume()");
LocationUtils.checkAndEnableLocationServices(this);
}
我缺少什麼?爲什麼這是對話框不關閉?該代碼不會引發任何錯誤。這是我的一個WTF時刻。
沒有工作。已經試過了 –