我正在开发的应用程序的用户需要在注册过程中共享位置。点击“定位我”按钮应该会得到GPS坐标(或任何提供的,我还不知道)。
单击该按钮时,我会收到以下错误消息:

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

据我所知,这与在清单文件中列出权限有关。但许可就在那里。
我发现有类似问题的线程建议从我正在测试的设备上卸载应用程序,清理并重建项目。我做了所有这些,但问题依然存在。我肯定还漏掉了什么,可能很明显。有什么想法吗?
谢谢!
这是我的androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest package = "com.iskillu.iskillu"
      xmlns:android = "http://schemas.android.com/apk/res/android">

    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23"/>

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


    <application
        android:allowBackup = "true"
        android:icon = "@mipmap/ic_launcher"
        android:label = "@string/app_name"
        android:supportsRtl = "true"
        android:theme = "@style/AppTheme">
        <activity
            android:name = ".MainActivity"
            android:label = "@string/app_name"
            android:theme = "@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name = "android.intent.action.MAIN" />

                <category android:name = "android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<!--
            ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
-->
        <meta-data
            android:name = "com.google.android.gms.version"
            android:value = "@integer/google_play_services_version" />

        <activity
            android:name = ".JoinActivity"
            android:label = "@string/title_activity_join"
            android:theme = "@style/AppTheme.NoActionBar">
        </activity>

        <activity
            android:name = ".LoginActivity"
            android:label = "@string/title_activity_login"
            android:theme = "@style/AppTheme.NoActionBar">
        </activity>

        <activity android:name = ".RegistrationSuccessfulActivity">
            <intent-filter>
                <action android:name = "android.intent.action.VIEW" />

                <category android:name = "android.intent.category.DEFAULT" />
                <category android:name = "android.intent.category.BROWSABLE" />

                <data android:scheme = "http" />
                <data android:scheme = "https" />
                <data android:host = "www.iskillu.com" />
                <data android:host = "iskillu.com" />
            </intent-filter>
        </activity>
        <activity android:name = ".presentation.PresentationActivity">
        </activity>
    </application>
</manifest>

这是执行时引发异常的方法:
    public void getGpsLocation ( View view )
        {
            LocationManager locationManager = ( LocationManager )        this.getSystemService( Context.LOCATION_SERVICE );

            boolean isProviderEnabled = locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER);

            if ( ! locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER) )
            {
                Toast.makeText ( this.getApplicationContext (), R.string.enable_gps, Toast.LENGTH_LONG ).show () ;
            }
            else
            {
                try {
                    Location myLocation =         locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if (myLocation != null)
                    {
                        Log.d("TAG", "Not null");
                    }
                    else
                    {
                        Log.d("TAG", "NULL");
                    }
                }
                catch (SecurityException se) {
                    Log.d("TAG", "SE CAUGHT");
                    se.printStackTrace();
                }
            }
        }

最佳答案

从android m开始,您需要在运行时请求许可Requesting Permissions at Run Time

09-09 20:57