Difficulty with Permission Location Android

0

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

The code I'm using can be found here: link

/**
 * 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 'this' of the permission, which has the following error:

  

checkSelfPermission (android.content.Context, String) in   ContextCompat can not be applied

     

to (anonymous   rx.Observable.OnSubscribe, String)

I do not know how to get this to work, if anyone can give me a helping hand thanks.

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;
                }

I'm not sure how to do this,

  

checkSelfPermission (android.content.Context, String) in ContextCompat can not be applied   to (anonymous rx.Observable.OnSubscribe, String)

    
asked by anonymous 20.11.2016 / 15:40

2 answers

1

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

ActivityCompat .checkSelfPermission (Context, String) receives in the 1st parameter an Context object. .

The code is using this as an argument to this parameter:

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

this , within an instance or constructor method, refers to the current object.
In order to be able to use% w / w, this object must be Context .

The method call is being made within a method of an anonymous (interface) class of type Observable.OnSubscribe . Using ActivityCompat.checkSelfPermission() in this context, you are passing an Observable.OnSubscribe object when a Context type is expected, hence the error:

  

checkSelfPermission (android.content.Context, String) in ContextCompat can not be applied   to (anonymous rx.Observable.OnSubscribe, String)

For the LocationService class to be able to use the this method, you must have a Context available. One possible way to make it available is to pass it on to the builder,

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

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

    ...
    ...
}

which can be used in the ActivityCompat.checkSelfPermission() method like this:

if (ActivityCompat.checkSelfPermission(mContext,
           Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
    ActivityCompat.checkSelfPermission(mContext,
           Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
    ...
    ...
}
    
21.11.2016 / 15:29
0

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

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

Take a look at in this answer , which will get you better informed.

    
20.11.2016 / 17:03