0

I tried to make an application that is using location. When I want location update an error appears and says "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"

That's what I tried to put in my code

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

But after I put that I got an error on LocationService which says "Cannot resolve symbol LocationService".

Here is my code

    public void onCreate() {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        contentIntent = PendingIntent.getActivity(
                this, 0, notificationIntent, 0);

        //updateNotification(false);
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.addGpsStatusListener(this);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);

    }
Paul
  • 11
  • 2
  • Can you check [this answer](https://stackoverflow.com/a/42890743/876267) to similar question? – Mehmed Jul 04 '19 at 18:29

2 Answers2

0

Ask for permission at runtime to use device current location as below :

    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{
      // Write you code here if permission already given.
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.addGpsStatusListener(this);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
    } 

Handle when user allow to use device current location or not :

       @Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
               if (grantResults.length > 0  && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Write you code here if permission already given.
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.addGpsStatusListener(this);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
    }                           }else{
                            // you must ask location permission again
                            }
    }
Mahmoud Waked
  • 357
  • 2
  • 8
0

In your manifest file set

android.permission.ACCESS_COARSE_LOCATION

permission for Network provider, or

android.permission.ACCESS_FINE_LOCATION

for GPS provider.

TechDuck
  • 17
  • 6