由以下原因引起:java.lang.SecurityException:"gps"位置提供者需要ACCESS_FINE_LOCATION权限 [英] Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission

查看:1650
本文介绍了由以下原因引起:java.lang.SecurityException:"gps"位置提供者需要ACCESS_FINE_LOCATION权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置权限.为什么我仍然会收到此错误?

由以下原因引起:java.lang.SecurityException:"gps"位置提供程序需要ACCESS_FINE_LOCATION权限.

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

我的清单内容:

    <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" />

我的活动代码:

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

推荐答案

您的 targetSdkVersion 是什么?

在面向Android 6.0(API级别23)或更高版本的应用上,请确保在运行时检查并请求权限.要确定您的应用是否已被授予权限,请调用新的checkSelfPermission()方法.要请求权限,请调用新的requestPermissions()方法.即使您的应用程序未定位到Android 6.0(API级别23),也应在新的权限模型下测试您的应用程序. :从Android网站

在Android 6.0 Marshmallow中,应用程序在安装时不会被授予任何权限.取而代之的是,只有在应用程序目标级别为23 时,应用程序才必须在运行时逐一询问用户权限.

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.

对于旧版本:
如果应用程序的targetSdkVersion设置为小于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 !

这篇关于由以下原因引起:java.lang.SecurityException:"gps"位置提供者需要ACCESS_FINE_LOCATION权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆