Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission

I already set permission. Why do I still get this error?

Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.

My manifest contents:

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

My Activity code:

    public class LocationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }




    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        private LocationManager mLocationManager;

        public PlaceholderFragment() {
        }

        private final LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
            txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());

            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_location, container, false);

            mLocationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);

            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,            locationListener);

            return rootView;
        }
    }
}
like image 431
le an Avatar asked Oct 29 '14 08:10

le an


1 Answers

What is your targetSdkVersion?

On your apps that target Android 6.0 (API level 23) or higher, make sure to check for and request permissions at runtime. To determine if your app has been granted a permission, call the new checkSelfPermission() method. To request a permission, call the new requestPermissions() method. Even if your app is not targeting Android 6.0 (API level 23), you should test your app under the new permissions model. : From Android Site

In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime only if application target level is 23.

For older version:
If the application's targetSdkVersion is set to less than 23. It will be assumed that application is not tested with new permission system yet and will switch to the same old behavior: user has to accept every single permission at install time and they will be all granted once installed !

like image 175
Govinda Paliwal Avatar answered Nov 08 '22 16:11

Govinda Paliwal