我正在开发一个使用Google Play服务的应用程序。在一些手机上,客户端返回位置为null。这是因为Google设置中未启用位置选项,如所附图片。如何在Android应用程序中编程检查Google设置位置是否已启用?请参考以下链接:http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/。
/**
* This is used to check whether location is enabled or not.
* @param mContext
* @return
*/
public static boolean isDeviceLocationEnabled(Context mContext) {
int locMode = 0;
String locProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locMode = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locProviders = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locProviders);
}
}
应该有一个更改,您需要检查TextUtils.isEmpty(locationProviders)
。它永远不为空。当没有提供程序(位置设置已禁用)时,它的值为"null"
。是的,我指的是值"null"
,而不是null
。
LocationManager
来检查不同的提供者。 - tyczj