I am new to Android Studio and am trying to retrieve the co-ordinates of an Android device using Location Service.
I have all the relevant permissions enabled in the manifest (the Coarse and Fine locations, and the Internet).
In my MainActivity.java, I have the following. However, when I run it in Android Studio, for the 3 test cases I have in the activity, the TextView ShowLocation always become "Something else", meaning that it went to the third case where location is null (no location detected?) but permission has been granted properly.
What am I doing wrong here? Is it because I could not test Location Service in Android Studio? How can I test and obtain co-ordinate values in an Android application using Android Studio?
A snippet of MainActivity.java
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
latitude = String.valueOf(loc.getLatitude());
showLocation.setText("Latitude" + latitude);
}
else {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
showLocation.setText("No GPS Permission granted");
} else
showLocation.setText("Something else");
}