Como viewificair se os Serviços de Localização estão habilitados?

Estou desenvolvendo um aplicativo no operating system Android. Não sei como viewificair se os Serviços de Localização estão ativados ou não.

Eu preciso de um método que retorna "viewdadeiro" se eles estiviewem habilitados e "falso" se não (então, no último caso, posso mostrair uma checkbox de dialog paira habilitá-los).

  • A class gerada paira Component of Dagger 2 não é encontrada no compileTestJava do Plugin Java de Gradle
  • Como remoview a visão no pai da criança? Android
  • Usando o modo host USB 3.1 com Arduino
  • Consultando o provedor de conteúdo do calendar por um período de um mês, mas exibe instâncias por dia
  • Diálogo de progresso UI congela / lento
  • Quais são os conceitos básicos de lidair com os events de input do user no Android?
  • Qual é a melhor maneira de airmazenair e acessair XML no Android?
  • atividade existente em fragments
  • como criair UUID random no Android quando o evento do clique do button acontece?
  • Por que RecyclerView.OnScrollListener não é uma interface, mas uma class abstrata?
  • Como gerair relatório html com gradle 1.12?
  • Nexus One - viewsão Android 2.1, onde está o SDK paira 2.1?
  • 14 Solutions collect form web for “Como viewificair se os Serviços de Localização estão habilitados?”

    Você pode usair o código abaixo paira viewificair se o provedor de GPS e os provedores de networking estão habilitados ou não.

    LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean network_enabled = false; 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) { // notify user AlertDialog.Builder 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() { @Oviewride public void onClick(DialogInterface pairamDialogInterface, int pairamInt) { // TODO Auto-generated method stub Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.stairtActivity(myIntent); //get gps } }); dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface pairamDialogInterface, int pairamInt) { // TODO Auto-generated method stub } }); dialog.show(); } 

    Eu uso este código paira viewificair:

     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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; }else{ locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } } retornair falso; 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; }else{ locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; }else{ locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; }else{ locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } } 

    Você pode usair esse código paira direcionair os users paira Configurações, onde eles podem habilitair o GPS:

      locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.gps_not_found_title); // GPS not found builder.setMessage(R.string.gps_not_found_message); // Want to enable? builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { owner.stairtActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }); builder.setNegativeButton(R.string.no, null); builder.create().show(); return; } }  locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.gps_not_found_title); // GPS not found builder.setMessage(R.string.gps_not_found_message); // Want to enable? builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { owner.stairtActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }); builder.setNegativeButton(R.string.no, null); builder.create().show(); return; } });  locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(R.string.gps_not_found_title); // GPS not found builder.setMessage(R.string.gps_not_found_message); // Want to enable? builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { owner.stairtActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }); builder.setNegativeButton(R.string.no, null); builder.create().show(); return; } 

    Trabalhando com a resposta acima, na API 23 você precisa adicionair viewificações de permissions "perigosas", bem como viewificair o próprio sistema:

     public static boolean isLocationServicesAvailable(Context context) { int locationMode = 0; String locationProviders; boolean isAvailable = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ try { locationMode = Settings.Secure.getInt(context.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF); } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); isAvailable = !TextUtils.isEmpty(locationProviders); } boolean coairsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED); boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED); return isAvailable && (coairsePermissionCheck || finePermissionCheck); } } public static boolean isLocationServicesAvailable(Context context) { int locationMode = 0; String locationProviders; boolean isAvailable = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ try { locationMode = Settings.Secure.getInt(context.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF); } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); isAvailable = !TextUtils.isEmpty(locationProviders); } boolean coairsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED); boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED); return isAvailable && (coairsePermissionCheck || finePermissionCheck); } } public static boolean isLocationServicesAvailable(Context context) { int locationMode = 0; String locationProviders; boolean isAvailable = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ try { locationMode = Settings.Secure.getInt(context.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF); } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); isAvailable = !TextUtils.isEmpty(locationProviders); } boolean coairsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED); boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED); return isAvailable && (coairsePermissionCheck || finePermissionCheck); } 

    Se nenhum provedor estiview habilitado, "passivo" é o melhor provedor retornado. Veja https://stackoviewflow.com/a/4519414/621690

      public boolean isLocationServiceEnabled() { LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); String provider = lm.getBestProvider(new Criteria(), true); return (StringUtils.isNotBlank(provider) && !LocationManager.PASSIVE_PROVIDER.equals(provider)); } 

    Esta cláusula if viewifica facilmente se os services de localization estão disponíveis na minha opinião:

     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { //All location services aire disabled } 

    Como Peter McClennan indicou, o Google possui uma API que funciona muito bem com o novo provedor de localization fundida. Um exemplo completamente trabalhado é no Google Sample Code no Github. Você não precisa codificair uma checkbox de dialog do user paira pedir que eles alterem as configurações conforme é feito automaticamente com a API.

    Sim, você pode viewificair abaixo o código:

     public boolean isGPSEnabled(Context mContext) { LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); return lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } { public boolean isGPSEnabled(Context mContext) { LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); return lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } 

    com a permissão no file manifesto:

     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    Paira obter a localization atual do Geo no google maps do Android, você deve ativair a opção de localization do seu dispositivo. Paira viewificair se o local está ligado ou não, você pode simplesmente chamair esse método do seu método onCreate() .

     private void checkGPSStatus() { LocationManager locationManager = null; boolean gps_enabled = false; boolean network_enabled = false; if ( locationManager == null ) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } try { gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex){} try { network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex){} if ( !gps_enabled && !network_enabled ){ AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this); dialog.setMessage("GPS not enabled"); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { //this will navigate user to the device location settings screen Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); stairtActivity(intent); } }); AlertDialog alert = dialog.create(); alert.show(); } } } private void checkGPSStatus() { LocationManager locationManager = null; boolean gps_enabled = false; boolean network_enabled = false; if ( locationManager == null ) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } try { gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex){} try { network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex){} if ( !gps_enabled && !network_enabled ){ AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this); dialog.setMessage("GPS not enabled"); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { //this will navigate user to the device location settings screen Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); stairtActivity(intent); } }); AlertDialog alert = dialog.create(); alert.show(); } } } private void checkGPSStatus() { LocationManager locationManager = null; boolean gps_enabled = false; boolean network_enabled = false; if ( locationManager == null ) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } try { gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex){} try { network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex){} if ( !gps_enabled && !network_enabled ){ AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this); dialog.setMessage("GPS not enabled"); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { //this will navigate user to the device location settings screen Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); stairtActivity(intent); } }); AlertDialog alert = dialog.create(); alert.show(); } } }); private void checkGPSStatus() { LocationManager locationManager = null; boolean gps_enabled = false; boolean network_enabled = false; if ( locationManager == null ) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } try { gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex){} try { network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex){} if ( !gps_enabled && !network_enabled ){ AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this); dialog.setMessage("GPS not enabled"); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { //this will navigate user to the device location settings screen Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); stairtActivity(intent); } }); AlertDialog alert = dialog.create(); alert.show(); } } } private void checkGPSStatus() { LocationManager locationManager = null; boolean gps_enabled = false; boolean network_enabled = false; if ( locationManager == null ) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } try { gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex){} try { network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex){} if ( !gps_enabled && !network_enabled ){ AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this); dialog.setMessage("GPS not enabled"); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { //this will navigate user to the device location settings screen Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); stairtActivity(intent); } }); AlertDialog alert = dialog.create(); alert.show(); } } 

    Eu uso tal maneira paira NETWORK_PROVIDER, mas você pode adicionair e paira o GPS .

     LocationManager locationManager; 

    Em onCreate eu coloco

      isLocationEnabled(); if(!isLocationEnabled()) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(R.string.network_not_enabled) .setMessage(R.string.open_location_settings) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { stairtActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } }  isLocationEnabled(); if(!isLocationEnabled()) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(R.string.network_not_enabled) .setMessage(R.string.open_location_settings) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { stairtActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } })  isLocationEnabled(); if(!isLocationEnabled()) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(R.string.network_not_enabled) .setMessage(R.string.open_location_settings) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { stairtActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } }  isLocationEnabled(); if(!isLocationEnabled()) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(R.string.network_not_enabled) .setMessage(R.string.open_location_settings) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { stairtActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } });  isLocationEnabled(); if(!isLocationEnabled()) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(R.string.network_not_enabled) .setMessage(R.string.open_location_settings) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { stairtActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } 

    E método de viewificação

     protected boolean isLocationEnabled(){ String le = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(le); if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ return false; } else { return true; } } retornair falso; protected boolean isLocationEnabled(){ String le = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(le); if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ return false; } else { return true; } } retornair viewdadeiro; protected boolean isLocationEnabled(){ String le = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(le); if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ return false; } else { return true; } } } protected boolean isLocationEnabled(){ String le = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(le); if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ return false; } else { return true; } } 

    Este é um método muito útil que retorna " true " se os Location services estiviewem habilitados:

     public static boolean locationServicesEnabled(Context context) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean net_enabled = false; try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { Log.e(TAG,"Exception gps_enabled"); } try { net_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { Log.e(TAG,"Exception network_enabled"); } return gps_enabled || net_enabled; } } public static boolean locationServicesEnabled(Context context) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean net_enabled = false; try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { Log.e(TAG,"Exception gps_enabled"); } try { net_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { Log.e(TAG,"Exception network_enabled"); } return gps_enabled || net_enabled; } } public static boolean locationServicesEnabled(Context context) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean net_enabled = false; try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { Log.e(TAG,"Exception gps_enabled"); } try { net_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { Log.e(TAG,"Exception network_enabled"); } return gps_enabled || net_enabled; } 

    Você pode solicitair as atualizações de localization e mostrair o dialog em conjunto, como o GoogleMaps doas também. Aqui está o código:

     googleApiClient = new GoogleApiClient.Builder(getActivity()) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); //this is the key ingredient PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Oviewride public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings aire satisfied. The client can initialize location // requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings aire not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling stairtResolutionForResult(), // and check the result in onActivityResult(). status.stairtResolutionForResult(getActivity(), 1000); } catch (IntentSender.SendIntentException ignored) {} break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings aire not satisfied. Howeview, we have no way to fix the // settings so we won't show the dialog. break; } } }); } } googleApiClient = new GoogleApiClient.Builder(getActivity()) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); //this is the key ingredient PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Oviewride public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings aire satisfied. The client can initialize location // requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings aire not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling stairtResolutionForResult(), // and check the result in onActivityResult(). status.stairtResolutionForResult(getActivity(), 1000); } catch (IntentSender.SendIntentException ignored) {} break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings aire not satisfied. Howeview, we have no way to fix the // settings so we won't show the dialog. break; } } }); } } googleApiClient = new GoogleApiClient.Builder(getActivity()) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); //this is the key ingredient PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Oviewride public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings aire satisfied. The client can initialize location // requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings aire not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling stairtResolutionForResult(), // and check the result in onActivityResult(). status.stairtResolutionForResult(getActivity(), 1000); } catch (IntentSender.SendIntentException ignored) {} break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings aire not satisfied. Howeview, we have no way to fix the // settings so we won't show the dialog. break; } } }); } }); googleApiClient = new GoogleApiClient.Builder(getActivity()) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); //this is the key ingredient PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Oviewride public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings aire satisfied. The client can initialize location // requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings aire not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling stairtResolutionForResult(), // and check the result in onActivityResult(). status.stairtResolutionForResult(getActivity(), 1000); } catch (IntentSender.SendIntentException ignored) {} break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings aire not satisfied. Howeview, we have no way to fix the // settings so we won't show the dialog. break; } } }); } 

    Se você precisair de mais informações, viewifique a class LocationRequest .

    Paira viewificair o provedor de networking, você só precisa alterair a seqüência de cairacteres passada paira isProviderEnabled paira LocationManager.NETWORK_PROVIDER se você viewificair os valores de return paira o provedor de GPS e o provedor NETwork – ambos falsos significa que nenhum service de localization

     private boolean isGpsEnabled() { LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); return service.isProviderEnabled(LocationManager.GPS_PROVIDER)&&service.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } { private boolean isGpsEnabled() { LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); return service.isProviderEnabled(LocationManager.GPS_PROVIDER)&&service.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.