【问题标题】:How to dismiss an AlertDialog when turning the Location on?打开位置时如何关闭 AlertDialog?
【发布时间】:2018-07-06 13:26:09
【问题描述】:

我正在创建一个简单的应用程序,其中我在地图上显示了一些标记。如果位置是off,我将使用以下代码显示AlertDialog

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//Set title, message and other stuff.

try {
    if (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE) == Settings.Secure.LOCATION_MODE_OFF) {
            alertDialog.show(); //Works perfect!
    }
} catch (Settings.SettingNotFoundException e) {e.printStackTrace();}

//Create a listener to check for Location Settings Changes
LocationListener locationListener = new LocationListener() {
    public void onProviderEnabled(String provider) {alertDialog.dismiss();} //Dismiss the AlertDialog
    // The other overloaded methods: onProviderDisabled, onLocationChanged and onStatusChanged
};

当我第一次启动应用程序时,位置变为offAlertDialog 出现。如果我打开on 的位置,如下图所示:

onProviderEnabled() 方法没有被触发。如果我关闭重新启动应用程序,一切正常。以下是我请求位置更新的方式。

if (isGooglePlayServicesOk()) {
    String[] permissions = {COARSE_LOCATION, FINE_LOCATION};
    if (COARSE LOCATION PERMISSION GRANTED CONDITION) {
        if (FINE LOCATION PERMISSION GRANTED CONDITION) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }
    }
}

请帮助我在应用程序第一次启动时关闭AlertDialog。谢谢!!

【问题讨论】:

    标签: android google-maps locationlistener


    【解决方案1】:

    方法没有被调用,因为应用程序已经跑过它并且发现 gps 已关闭,所以它没有关闭对话框。您可以启动一些线程 10 秒钟以再次检查或尝试收听位置提供程序的更改。为此,请查看此answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 2018-03-16
      • 2021-08-09
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多