Android: Google Settings Location Enabled

I am working on an application that uses Google Play services. On some phones, the client returns null for the location. This is because the locaiton parameter is not enabled in Google settings, as in the attached file.

How to programmatically check if google settings location is enabled in android app?

http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/

+6
source share
4 answers
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 shows the user settings that allow you to configure current location sources.

+4
source

You can check if google play services are available. Follow the instructions here: https://developer.android.com/training/location/retrieve-current.html

GetLastLocation () may return null even if Google Play location services are enabled. For example, immediately after their repeated inclusion. However, you can then send your user to the Google Play Services Location Settings using the following destination:

 Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS"); startActivity(settings); 

Finally, you can also check if Android location services are enabled:

 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); } } 
+9
source

I thought this was the best solution.

You can verify by running the following code:

  /** * 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); } } 
+1
source

There should be one change when you check for TextUtils.isEmpty(locationProviders) . It is never empty. If the provider is absent (location settings are disabled), it is set to "null" . Yes, I mean "null" , not null .

0
source

Source: https://habr.com/ru/post/970401/


All Articles