0

I have android application and we have recently shifted to API level 26 from 22.

I have cheked if the app has one of the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

One issue that I found and not able to solve is that it gives an exception like "java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission"

My Android manifest file has:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Also I got this kind of exception:

Process: edu.syr.ischool.orange.indoormap3, PID: 12666
    java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
        at android.os.Parcel.readException(Parcel.java:2005)
        at android.os.Parcel.readException(Parcel.java:1951)
        at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:802)
        at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1220)
        at utils.CheckinUtils.checkLocation(CheckinUtils.java:100)
        at utils.CheckinUtils.resume(CheckinUtils.java:247)

Also my checkinUtil --> checkLocation Method

 if (ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager = (LocationManager) activity.getSystemService(serviceString);
        Location lastKnow =  locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

I have checked for permission of either one of them as documented on https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String)

Still, It gives an exception for ACCESS_FINE_LOCATION but as it passes the if condition and gives the security exception so it should have the ACCESS_COARSE_LOCATION permission. Then Why the application is crashing?

Nilesh
  • 884
  • 3
  • 11
  • 22

3 Answers3

0

As of what I understood was, You are only checking for whether permission is granted or not.

If your app/code not ends if the permissions were not granted, make sure to request permissions by calling ...

ActivityCompat.requestPermissions(this, permissionsStringArray,requestCodeInt);

And also while checking self permissions try using ContextCompat instead of ActivityCompat

Naveen Avidi
  • 3,004
  • 1
  • 11
  • 23
0

Solution:

Instead of this line:

if (ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

Write this:

if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

You are taking the inbuilt permission from Android, that may be the bug.

You must take the permission from Manifest which you have mentioned there.

So instead of android.Manifest.permission.ACCESS_FINE_LOCATION, it should be just Manifest.permission.ACCESS_FINE_LOCATION

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
0

Android manifest file should be like this:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />

You can find more details here Request User Permissions And use your java code istead of : Location lastKnow = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); as below

  try {
    Location lastKnow1 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    Location lastKnow2 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location lastKnow3 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if(lastKnow1 != null){
       --- your code ---
    } if(lastKnow2 != null){
       --- your code ---
    } if(lastKnow3 != null){
       --- your code ---
    } else {
       --- Toast ---
    }
  } catch (Exception e) {
            e.printStackTrace();
          }

By using above code you will never face exception problems. ** HAPPY CODING **

Ashok
  • 55
  • 1
  • 10