如何在打开位置时关闭 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
,出现 AlertDialog
。如果我转动 on
Location 如下图所示:
没有触发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 秒以再次检查或尝试收听位置提供商的变化。为此,请查看此 answer.
我正在创建一个简单的应用程序,其中我在地图上显示一些标记。如果位置为 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
,出现 AlertDialog
。如果我转动 on
Location 如下图所示:
没有触发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 秒以再次检查或尝试收听位置提供商的变化。为此,请查看此 answer.