Moa*_*man 6 android location google-play-services
我正在开发一款使用Google Play服务的应用.在某些手机上,客户端为位置返回null.发生这种情况是因为在Google设置中未启用locaiton选项,如附带的图片中所示.
如何编程检查Android应用程序中是否启用了谷歌设置位置?
http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/
可以检查谷歌播放服务本身是否可用.按照以下说明操作:https: //developer.android.com/training/location/retrieve-current.html
即使启用了Google Play服务定位服务,getLastLocation()也可能返回null.例如,在重新启用它们之后.但是,您可以使用以下意图将用户带到Google Play服务位置设置:
Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS");
startActivity(settings);
Run Code Online (Sandbox Code Playgroud)
最后,还可以检查Android位置服务是否已启用:
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 0
应该有一项更改,您可以在其中进行检查TextUtils.isEmpty(locationProviders)。它永远不会空。如果没有提供者(位置设置被禁用),它就有价值"null"。是的,我的意思是价值"null",不是null。
| 归档时间: |
|
| 查看次数: |
9265 次 |
| 最近记录: |