Android 6.0 Method of Obtaining GPS Location and Obtaining Location Authority and Location Information

  • 2021-09-24 23:45:53
  • OfStack

1. Add permissions-get them dynamically after 6.0, as will be described below


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

2. Directly on the code, not to say, the code comments are very detailed.


private static final int BAIDU_READ_PHONE_STATE = 100;// Location permission request 
private static final int PRIVATE_CODE = 1315;// Open GPS Authority 

/**
 *  Detection GPS Is the location permission turned on 
 */
public void showGPSContacts() {
 lm = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
 boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
 if (ok) {// Location service was opened 
  if (Build.VERSION.SDK_INT >= 23) { // Determine whether it is android6.0 System version, if yes, need to dynamically add permissions 
   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
     != PERMISSION_GRANTED) {//  No permission, apply for permission. 
    ActivityCompat.requestPermissions(this, LOCATIONGPS,
      BAIDU_READ_PHONE_STATE);
   } else {
    getLocation();//getLocation For the positioning method 
   }
  } else {
   getLocation();//getLocation For the positioning method 
  }
 } else {
  Toast.makeText(this, " The system detected that it was not turned on GPS Location service , Please open ", Toast.LENGTH_SHORT).show();
  Intent intent = new Intent();
  intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  startActivityForResult(intent, PRIVATE_CODE);
 }
}

/**
 *  Get the latitude and longitude of a specific location 
 */
private void getLocation() {
 //  Obtaining Location Management Services 
 LocationManager locationManager;
 String serviceName = Context.LOCATION_SERVICE;
 locationManager = (LocationManager) this.getSystemService(serviceName);
 //  Service information found 
 Criteria criteria = new Criteria();
 criteria.setAccuracy(Criteria.ACCURACY_FINE); //  High precision 
 criteria.setAltitudeRequired(false);
 criteria.setBearingRequired(false);
 criteria.setCostAllowed(true);
 criteria.setPowerRequirement(Criteria.POWER_LOW); //  Low power consumption 
 String provider = locationManager.getBestProvider(criteria, true); //  Get GPS Information 
 /** This code doesn't need to go deep into it, it is locationManager.getLastKnownLocation(provider) Automatically generated, error will occur if you don't add it **/
 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.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 = locationManager.getLastKnownLocation(provider); //  Pass GPS Get the location 
 updateLocation(location);
}

/**
 *  Get the latitude and longitude to the current position 
 * @param location
 */
private void updateLocation(Location location) {
 if (location != null) {
  double latitude = location.getLatitude();
  double longitude = location.getLongitude();
  LogUtil.e(" Dimensions: " + latitude + "\n Longitude " + longitude);
 } else {
  LogUtil.e(" Unable to get location information ");
 }
}
/**
 * Android6.0 Callback method for requesting permission 
 */
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 switch (requestCode) {
  // requestCode That is, the declared permission acquisition code, in the checkSelfPermission Pass in when 
  case BAIDU_READ_PHONE_STATE:
   // If the user cancels, permissions Could be null.
   if (grantResults[0] == PERMISSION_GRANTED && grantResults.length > 0) { // Have authority 
    //  Get the permission and deal with it accordingly 
    getLocation();
   } else {
    showGPSContacts();
   }
   break;
  default:
   break;
 }
}

onRequestPermissionsResult This method is mainly dynamic access to 6.0 permissions, callback when returning, I need here is access to the current position after access to the latitude and longitude details

3. The following is when you click to get GPS positioning, jump to the system switch, and call back ActivityResult. What I do here is to open GPS authority. If you don't open it, you will let the user open the authority directly. How to decide depends on the specific requirements


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

 }
}

4. Dynamic permission settings add multiple permissions


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

Note: The code is very detailed! The basic knowledge is not well written, so don't spray it, thank you!


Related articles: