Android: comprobación de si la ubicación de los ajustes de Google está habilitada
Estoy trabajando en una aplicación que utiliza servicios de Google Play. En algunos teléfonos, el cliente devuelve nulo para la ubicación. Esto sucede porque la opción de localización no está habilitada en la configuración de google como en la foto adjunta.
¿Cómo verificar programatically si la localización de los ajustes de google está permitida en una aplicación androide?
- Activar servicios de ubicación sin navegar a la página de configuración
- Cómo implementar onConnectionFailed () y onDisconnected () para un servicio de ubicación
- Cómo obtener el número de satélites del objeto Ubicación
- Iniciar la estrategia de ubicación
- Servicio de Android que no recibe Actualizaciones de ubicación
Http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/
- Cómo obtener la ubicación de Google Map después de soltar Pin sobre ella
- El emulador de Android 2.3 se bloquea al actualizar la ubicación
- Mapview NullPointerException android
- Problemas al utilizar Smart Location Library
- Localización de la ubicación: Servicio de ubicación de Google Play o API de ubicación de la plataforma Android
- Obtener latitud, longitud desde la dirección en Android
- ¿Es posible saber mi ubicación vino de gps o glonass?
- Cómo obtener el lugar actual mediante Google Place API
LocationManager lm = null; boolean gps_enabled,network_enabled; if(lm==null) lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try{ gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); }catch(Exception ex){} try{ network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); }catch(Exception ex){} if(!gps_enabled && !network_enabled){ dialog = new AlertDialog.Builder(context); dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled)); dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface paramDialogInterface, int paramInt) { // TODO Auto-generated method stub Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.startActivity(myIntent); //get gps } }); dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface paramDialogInterface, int paramInt) { // TODO Auto-generated method stub } }); dialog.show(); }
ACTION_LOCATION_SOURCE_SETTINGS muestra la configuración del usuario para permitir la configuración de los orígenes de ubicación actuales.
Es posible comprobar si los servicios de Google Play están disponibles. Siga las instrucciones aquí: https://developer.android.com/training/location/retrieve-current.html
Es posible que getLastLocation () devuelva null incluso si los Servicios de ubicación de servicios de Google Play están habilitados. Por ejemplo, justo después de volver a habilitarlos. Sin embargo, puede llevar a su usuario a los ajustes de ubicación de servicios de Google Play con la siguiente intención:
Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS"); startActivity(settings);
Por último, también es posible comprobar si los Servicios de ubicación de Android están habilitados:
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); } }
Debe haber un cambio, donde se comprueba TextUtils.isEmpty(locationProviders)
. Nunca está vacío. Donde no hay proveedor (la configuración de ubicación está deshabilitada) tiene valor "null"
. Sí me refiero a valor "null"
, no null
.
Pensé que esta es la mejor solución.
Puede comprobar mediante el siguiente código:
/** * 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); } }
- Android: ¿Cómo llamar a onTouch sólo después de un onLongClick?
- Algo como Android-SharedPreferences en Windows Store Apps?