-2

Used this code on previous app and all seems ok - but today im getting a

call requires permission which may be rejected by user

I have added

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

to manifest and have read several threads on the subject, but still having issues. Could you guys helps out.

public class MainActivity extends AppCompatActivity implements LocationListener
{
    private LocationManager locationManager;
    public static String locationlong;
    public static String locationlati;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView)findViewById(R.id.pig);
        TextView cow = (TextView)findViewById(R.id.cow);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
    }
}
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

2 Answers2

0

Basically since android 6 you need to ask certain permissions on runtime , so the user may reject them.

Use checkSelfPermission(..) to ask for it and override onRequestPermissionsResult to see the answer.

Here's a link with the full example:

https://developer.android.com/training/permissions/requesting.html

yanivtwin
  • 617
  • 8
  • 32
  • Thanks for this. Read a few threads and seem to have problem implementing. Came across this int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR); but unsure what to use for thisActivity – Clair Jones Apr 23 '17 at 16:04
  • Well it depends where are you implemening it , you basically need the Activity context , if you're using it in an activity simply use "this" to get the activity reference , or getActivity() in a fragment and so on... – yanivtwin Apr 23 '17 at 16:12
0

Add the permission check in your activity:

in your code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.pig);
    TextView cow = (TextView) findViewById(R.id.cow);
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
    }
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (checkLocationPermission()) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
    }


}

public boolean checkLocationPermission() {
    String permission = "android.permission.ACCESS_FINE_LOCATION";
    int res = this.checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {

        case 0: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

@Override
public void onLocationChanged(Location location) {

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Thanks for the help, but will that warning appear appear only the once, also I have code that does not have this checking. How come I need it on some and not others. – Clair Jones Apr 23 '17 at 16:28
  • the warning will appear when you set your target or compile sdk version to 23 or above in manifest..and android has 2 types of permission normal and dangerous. on 'dangerous` permission you need to request permission in runtime and also check if the permission is granted or not.. – rafsanahmad007 Apr 23 '17 at 16:31
  • here is the list: http://stackoverflow.com/questions/36936914/list-of-android-permissions-normal-permissions-and-dangerous-permissions-in-api – rafsanahmad007 Apr 23 '17 at 16:32