Aman Verma Aman Verma - 1 year ago 1865
Android Question

Call requires permissions that may be rejected by user

I am trying to make an application that sends location updates of a user after every five minutes. I suppose my code is working just fine but i get an error regarding the permissions that are being used by the application. I am pretty sure that i have added the permissions in the manifest file. Can someone tell me what's wrong? Here is my code.

LocationManager locationManager ;
String provider;

public void onCreate(Bundle savedInstanceState) {

// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

// Creating an empty criteria object
Criteria criteria = new Criteria();

// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);

if(provider!=null && !provider.equals("")){

// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);

Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
return true;

public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(;

// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView)findViewById(;

// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());

// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude() );

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub


I am getting an error as Call requires permission which may be rejected by user in these lines-

Location location = locationManager.getLastKnownLocation(provider);

My AndroidManifest is like this

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

android:theme="@style/AppTheme" >
android:label="@string/title_activity_main" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

Answer Source

Which SDK do you use? If you use Marshmallow, then you need to check that the user has granted permission for every location call.

Take a look Here.

You should do something like this:

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

request permission if you dont have it already.

check the link above for more info.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download