this is my code for getting location :
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();
When I test and debug this on Android 4.4.2, it works well and it is debuggable but when it's run on Android 8 it seems code does not execute and is not debuggable
How can I get this to work on Android 8?
Try This
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
Toast.makeText(this, "Location is needed...", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1234);
}
} else {
//Permission is already granted. Get your location here
}
} else {
//Do your stuff here for SDK below 23
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1234:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do your work
} else {
Toast.makeText(this, "Location is needed...", Toast.LENGTH_SHORT).show();
}
break;
}
}