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

我与影子孤独终老i 提交于 2019-12-22 06:37:22

问题


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

回答1:


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 !




回答2:


The problem could be because you put the permissions before the application tags.Try putting the permissions after the application tag i.ejust before the closing manifest tag.



来源:https://stackoverflow.com/questions/26625363/caused-by-java-lang-securityexception-gps-location-provider-requires-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!