Skip to content

Instantly share code, notes, and snippets.

@MohammadSamandari
Created March 31, 2019 16:03
Show Gist options
  • Save MohammadSamandari/b2f04f69e918c831a75466a8fc965657 to your computer and use it in GitHub Desktop.
Save MohammadSamandari/b2f04f69e918c831a75466a8fc965657 to your computer and use it in GitHub Desktop.
Learning: Getting users location - Asking for permission to access location
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// We are going to write Code to get Users Location:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
// This Listen For Location Changes.
@Override
public void onLocationChanged(Location location) {
// This Happens When Phone Moves.
Log.i("Lord", String.valueOf(location));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// This Happens when location service is enables Or disabled.
}
@Override
public void onProviderEnabled(String provider) {
// This Happens when location service is enabled By user.
}
@Override
public void onProviderDisabled(String provider) {
// This Happens when location service is disabled By user.
}
};
// If Device is running SDK < 23 ( This means Running android before MarshMellow )
// In This Case no checking is needed because the minSDKVersion is 23 but any way it is check like below.
if(Build.VERSION.SDK_INT<23){
Log.i("Lord", String.valueOf(Build.VERSION.SDK_INT));
}
// Since Android MarshMellow(6) we need to Exactly request users Permission to use their location.
// We start by checking weather or not we have permission:
// We are checking if we have this Permission or Not.
// ContextCompat (ContextCompatibility) Class : this makes our app compatible with pre-MarshMellow versions of android.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// So this codes happen if we don't have permission,
// Asking for permission:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
//This Integer (1) is essentially a number that we can check at the other end that it was this particular request that was made.
} else {
// We Have Permission so we ask for location.
Log.i("Lord", "CheckSelfPermission Already Has Permission");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
// We are not doing anything after the permission asked, so to do that we have to prepare another method which is below.
// This is a method that already exist, so we have to override it.
// Request Code: the integer that we defined in the requestPermission part above.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.i("Lord", "onRequestPermissionsResult");
Log.i("Lord", String.valueOf(grantResults.length) + " " + grantResults[0]);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// If both of condition above is true, then we have Permission.
// So if we have permission, we can start listening for the users location. to do that we need LocationManger.
// To actually do the Listening for the users location:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment