3

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.

swordguy8
  • 167
  • 1
  • 10
  • This is a duplicate. Now that I know, what can I do about it? Is deletion acceptable? – swordguy8 Feb 14 '17 at 00:50
  • Well I changed the question a bit since the permissions thing didn't work (permissions were granted in the app, but the code which wasn't running still won't run). Is this still technically a duplicate? – swordguy8 Feb 14 '17 at 01:34

1 Answers1

3

Starting with Android 6.0 (API level 23) Google requires developers to request potentially dangerous permissions at run time:

ActivityCompat.requestPermissions(MainMenu.this, 
        Manifest.permission.ACCESS_FINE_LOCATION, 
        GET_PERMISSIONS_REQUEST_CODE
);

This shows a pop-up asking for permission to access the user's location. Any location requests you make prior will return empty.

Nathan Bird
  • 910
  • 1
  • 9
  • 23
  • And this is true even if I grant permission manually? – swordguy8 Feb 14 '17 at 00:03
  • @swordguy8 Yes, It was implemented to prevent app developers from 'sneaking' permissions like location into their apps without actually 'needing' them. – Nathan Bird Feb 14 '17 at 00:09
  • Neat! Thank you. Off to the tutorials... – swordguy8 Feb 14 '17 at 00:21
  • Now I've got it so it's asking for permissions, and I'm granting them, but I'm still getting that section of code not running. I just posted the code I put in. – swordguy8 Feb 14 '17 at 01:21
  • @swordguy8 I have two possible answers; First, make sure the device you are using for testing has its location enabled. Look for the location pin to appear when you open your app to make sure your app is properly accessing the location. – Nathan Bird Feb 14 '17 at 23:02
  • Also, are you running your code 1 time, or is it in a loop? Sometimes the location manager gives you lat/long data with values of 0 for the first few seconds. You need to make your app retrieve the data several times, not just once. If it still doesn't work, shake your device around or take a walk. Sometimes the device just doesn't co-operate at first. – Nathan Bird Feb 14 '17 at 23:06
  • 1
    Thank you. Location is definitely enabled. And it's not running in a loop, but I tried pushing the button multiple times. Also opened Google maps and my location was displayed perfectly and instantly there. – swordguy8 Feb 16 '17 at 00:36
  • @swordguy8 strange... I don't see what's wrong with your code then... I could just put my code on github if you never find a solution. ;) Let me know if I can help! – Nathan Bird Feb 16 '17 at 01:11
  • 1
    I think I figured it out! After way more research and looking at multiple code examples, I figured out the most bare bones way to do this. Where should I post the solution? Even though this is a duplicate question, I looked at the original and it didn't help me either. I'd like to help others who may have this issue. – swordguy8 Feb 17 '17 at 02:55
  • @swordguy8 I would recommend "Answering your own question" If you do this, go ahead and include my answer in yours so others can see if they forgot permissions as well. – Nathan Bird Feb 18 '17 at 18:16