繁体   English   中英

打开位置时如何关闭AlertDialog?

[英]How to dismiss an AlertDialog when turning the Location on?

我正在创建一个简单的应用,其中在地图上显示了一些标记。 如果位置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
};

当我在off位置的情况下首次启动该应用程序off ,将显示AlertDialog 如果我把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 谢谢!!

方法未调用,因为应用程序已经运行了该方法,并且发现gps已关闭,因此未关闭该对话框。 您可以启动某个线程10秒钟左右以再次检查,也可以尝试收听位置提供者的更改。 为此,请查看此答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM