SoFunction
Updated on 2025-03-01

Android 6.0 method to obtain GPS positioning and obtain location permissions and location information

1. Add permissions---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

2. Put the code directly on, and don’t say much, the comments in the code are very detailed.

private static final int BAIDU_READ_PHONE_STATE = 100;//Location permission requestprivate static final int PRIVATE_CODE = 1315;//OpenGPSPermissions
/**
  * Check whether GPS and location permissions are enabled
  */
public void showGPSContacts() {
 lm = (LocationManager) (this.LOCATION_SERVICE);
 boolean ok = (LocationManager.GPS_PROVIDER);
 if (ok) {//Location service has been opened  if (.SDK_INT &gt;= 23) { //Distinguish whether it is the Android 6.0 system version. If so, you need to add permissions dynamically   if ((this, .ACCESS_FINE_LOCATION)
     != PERMISSION_GRANTED) {// No permission, apply for permission.    (this, LOCATIONGPS,
      BAIDU_READ_PHONE_STATE);
   } else {
    getLocation();//getLocation is the location method   }
  } else {
   getLocation();//getLocation is the location method  }
 } else {
  (this, "The system detects that the GPS location service is not enabled, please enable it", Toast.LENGTH_SHORT).show();
  Intent intent = new Intent();
  (Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  startActivityForResult(intent, PRIVATE_CODE);
 }
}

/**
  * Get the latitude and longitude of the specific location
  */
private void getLocation() {
 // Obtain location management services LocationManager locationManager;
 String serviceName = Context.LOCATION_SERVICE;
 locationManager = (LocationManager) (serviceName);
 // Find service information Criteria criteria = new Criteria();
 (Criteria.ACCURACY_FINE); // High precision (false);
 (false);
 (true);
 (Criteria.POWER_LOW); // Low power consumption String provider = (criteria, true); // Get GPS information /**This code does not require any further investigation. It is automatically generated by (provider). If you do not add it, you will get an error**/
 if ((this, .ACCESS_FINE_LOCATION) != PERMISSION_GRANTED &amp;&amp; (this, .ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
  // TODO: Consider calling
  // ActivityCompat#requestPermissions
  // here to request the missing permissions, and then overriding
  // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  //           int[] grantResults)
  // to handle the case where the user grants the permission. See the documentation
  // for ActivityCompat#requestPermissions for more details.
  return;
 }
 Location location = (provider); // Get location via GPS updateLocation(location);
}

/**
  * Get the latitude and longitude of the current location
  * @param location
  */
private void updateLocation(Location location) {
 if (location != null) {
  double latitude = ();
  double longitude = ();
  ("Dimension:" + latitude + "\nLongitude" + longitude);
 } else {
  ("Cannot obtain location information");
 }
}
/**
  * Callback method for Android 6.0 to apply for permissions
  */
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
 (requestCode, permissions, grantResults);
 switch (requestCode) {
  // requestCode is the declared permission obtaining code, which is passed in when checkSelfPermission  case BAIDU_READ_PHONE_STATE:
   //If the user cancels, permissions may be null.   if (grantResults[0] == PERMISSION_GRANTED &amp;&amp;  &gt; 0) { //Available for permission    // Obtain permissions and handle them accordingly    getLocation();
   } else {
    showGPSContacts();
   }
   break;
  default:
   break;
 }
}

onRequestPermissionsResult This method mainly dynamically obtains 6.0 permissions, and calls back when returning. My need here is to obtain the permissions and get the latitude and longitude details of the current location.

3. The following is when clicking to obtain GPS positioning, jump to the system switch, ActivityResult callback. What I did here is that GPS permission must be enabled. If it is not enabled, the user will always be enabled. How to decide depends on the specific needs.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 (requestCode, resultCode, data);
 switch (requestCode) {
  case PRIVATE_CODE:
    showContacts();
   break;

 }
}

4. Dynamic permission settings add multiple permissions

static final String[] LOCATIONGPS = new String[]{.ACCESS_COARSE_LOCATION, 
  .ACCESS_FINE_LOCATION, 
  .READ_PHONE_STATE};

Note:The code is very detailed! If you don’t write the basic knowledge well, please don’t criticize me, thank you!

The above method of obtaining GPS positioning and obtaining location permissions and location information in Android 6.0 is all the content I share with you. I hope you can give you a reference and I hope you can support me more.