• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

LocationManager problem

grgp89

Lurker
Hello guys!
I have the following code to request location update, but it doesn't work. I get this error "Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException".
Can you help me with some answers?
Thank you a lot!

The code that I have:
if (mLocationManager.getAllProviders().indexOf(LocationManager.GPS_PROVIDER) >= 0) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
} else {
Log.w("MainActivity", "No GPS location provider found. GPS data display will not be available.");
}

if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
showGpsDisabledDialog();
}

mLocationManager.addGpsStatusListener(this);

PS: I included user-permission in AndroidManifest. I am running sdkVersion 27
 
You need to request permission
Java:
if (ContextCompat.checkSelfPermission( this,android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED )
{
    ActivityCompat.requestPermissions(
        this,
        new String [] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
        LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION
    );
}
 
Last edited:
Hello sir and thank you for your answer
First: I inserted the code you gave it to me before my code and it looks like this now
Java:
if (ContextCompat.checkSelfPermission( this,android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED )
        {
            ActivityCompat.requestPermissions(
                    this,
                    new String [] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
                    LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION
            );
        }


        if (mLocationManager.getAllProviders().indexOf(LocationManager.GPS_PROVIDER) >= 0) {


            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
        } else {
            Log.w("MainActivity", "No GPS location provider found. GPS data display will not be available.");
        }

        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showGpsDisabledDialog();
        }

        mLocationManager.addGpsStatusListener(this);

Second: Now I got an error at LocationService which says "Cannto resolve symbol 'LocationService'"
Could you help me in this case?
Or can you give me another method to resolve my problem?
Thank you!
 
That seems to be saying that LocationService isn't defined. Where are you defining or importing it?

I suspect it doesn't exist. I think @wseng92 may have led the OP down a blind alley. Could be wrong of course, but if we could see a complete example of using this LocationService, including import statements, that would be useful.

Failing that, I refer the OP to post #6, which details a complete example of how to request location permissions from the user at runtime.
 
Solved the problem like this
Java:
 if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
          return;
    }else{
     
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.addGpsStatusListener(this);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
    }

Thank for your efforts! Appreciate it!
 
Back
Top Bottom