I've searched for solutions to this and tried the ones I could find that were pertinent, but still can't get this to work. A specific section of code is not running. The issue I'm having is that this all works on my old NEC Terrain (4.0.1, API 15), but will not work on my new Blackberry Priv (6.0.1, API 23). In the newer phone, the first error log "Net enabled" shows up in logcat, and then none of the other error logs show up (I'm using them for troubleshooting). In the old phone, everything works and all the logs show up. What's stopping this code from running? I added code for adding permissions, which I've posted below. I also have the following in my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>
Here's the declaration for locationManager:
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
And here's the code:
if (isNetworkEnabled) {
Log.e("Tag", "Net Enabled");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e("Tag", "No permission");
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager == null) {
Log.e("Tag", "It's null.");
}
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.e("Tag", "NetLocMan OK");
if (location != null) {
Log.e("Tag", "NetLoc OK");
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.e("Tag", "" + latitude);
Log.e("Tag", "" + longitude);
}
}
}
if (isGPSEnabled) {
Log.e("Tag", "Location" + location);
if (location == null) {
Log.e("Tag", "GPSLoc finding");
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.e("Tag", "Location" + location);
if (locationManager != null) {
Log.e("Tag", "GPSLocMan OK");
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.e("Tag", "Location" + location);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.e("Tag", "" + latitude);
Log.e("Tag", "" + longitude);
}
}
}
}
This is the permissions code I'm using:
private static final int PERMS_REQUEST_CODE = 123;
if (!hasPermissions()){
requestPerms();
}
private boolean hasPermissions(){
int res = 0;
//string array of permissions,
String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
for (String perms : permissions){
res = checkCallingOrSelfPermission(perms);
if (!(res == PackageManager.PERMISSION_GRANTED)){
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean allowed = true;
switch (requestCode){
case PERMS_REQUEST_CODE:
for (int res : grantResults){
// if user granted all permissions.
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
// if user not granted permissions.
allowed = false;
break;
}
if (allowed){
//user granted all permissions we can perform our task.
}
else {
// we will give warning to user that they haven't granted permissions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){
Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
}
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)){
Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
}
}
}
}
Thanks for any help! Please let me know if you need more code.