Difficulty with Permission Location Android

Asked

Viewed 664 times

0

I’m trying to get a project up and running open-source found on the internet but I’m having some problems trying to implement the permissions for Location now required by compileSdkVersion 23 and buildToolsVersion "23.0.1" that I want to use.

The code I’m using can be found here: https://github.com/vyshane/rex-weather

/**
 * Implement an Rx-style location service by wrapping the Android LocationManager and providing
 * the location result as an Observable.
 */
public class LocationService {
    private final LocationManager mLocationManager;


    public LocationService(LocationManager locationManager) {
        mLocationManager = locationManager;
    }

    public Observable<Location> getLocation() {
        return Observable.create(new Observable.OnSubscribe<Location>() {
            @Override
            public void call(final Subscriber<? super Location> subscriber) {

                final LocationListener locationListener = new LocationListener() {
                    public void onLocationChanged(final Location location) {
                        subscriber.onNext(location);
                        subscriber.onCompleted();

                        Looper.myLooper().quit();
                    }

                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }

                    public void onProviderEnabled(String provider) {
                    }

                    public void onProviderDisabled(String provider) {
                    }
                };

                final Criteria locationCriteria = new Criteria();
                locationCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
                locationCriteria.setPowerRequirement(Criteria.POWER_LOW);
                final String locationProvider = mLocationManager
                        .getBestProvider(locationCriteria, true);

                Looper.prepare();

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mLocationManager.requestSingleUpdate(locationProvider,
                        locationListener, Looper.myLooper());

                Looper.loop();
            }
        });
    }
}

My problem is in the permission 'this', which has as error the following:

checkSelfPermission(android.content.Context,String)in Contextcompat cannot be Applied

to (Anonymous Rx.Observable.Onsubscribe,String)

I don’t know how to make this work.


I used the automatic add permission check and gave the following:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }

Only it gives error in "this" and I do not know how to solve it, and the resulting error is:

checkSelfPermission(android.content.Context,String)in Contextcompat cannot be Applied to(Anonymous Rx.Observable.Onsubscribe,)

2 answers

1

The difficulty is not in using permissions but in understanding what the keyword represents this.

Activitycompat.checkSelfPermission(Context, String) receives in the first parameter an object of the type Context.

The code is using this as argument for this parameter:

ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)

this, within an instance or constructor method, refers to the current object.
So I can use it in ActivityCompat.checkSelfPermission() the object must be of the Context.

The method call is being made within a method of an anonymous class(interface) of type Observable.Onsubscribe.
Using this in this context, an object such as Observable.Onsubscribe when a type is expected Context, hence the error:

checkSelfPermission(android.content.Context,String)in Contextcompat cannot be Applied to(Anonymous Rx.Observable.Onsubscribe,)

For class Locationservice be able to use the method ActivityCompat.checkSelfPermission() must have available a Context.
One possible way to make it available is to pass it on to the manufacturer,

public class LocationService {
    private final LocationManager mLocationManager;
    private final Context mContext;

    public LocationService(Context context, LocationManager locationManager) {
        mLocationManager = locationManager;
        mContext = context;
    }

    ...
    ...
}

which may be used in the method ActivityCompat.checkSelfPermission() thus:

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

0

Under conditions of Android 6.0(Api level 23) exists the Requesting Permissions at Run Time which is the issue of permissions, which is very interesting you give read and learn more. You have to use the requestPermissions to release permission to use the location:

ActivityCompat.requestPermissions((Activity) context, new String[]{
                                Manifest.permission.ACCESS_FINE_LOCATION},
                        REQUEST_PERMISSION_LOCALIZATION);

Take a look in this answer, that will clarify you better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.