【发布时间】: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
};
当我第一次启动应用程序时,位置变为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。谢谢!!
【问题讨论】:
标签: android google-maps locationlistener