Luke Wang Luke Wang - 4 months ago 251
Java Question

java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission

My manifest looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.lucas.myapp"
xmlns:android="http://schemas.android.com/apk/res/android">

<!--Internet is for internet. Access_fine_location is for GPS. You need both for GPS-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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


</intent-filter>
</activity>

<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

<activity
android:name=".SplashScreenWhite"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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


But I'm still getting this error from logcat


java.lang.SecurityException: "network" location provider requires
ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.


I'm trying to get user locations, so that's why I need GPS coordinates. Also, when I retrieve the GPS coordinates, they always come back as 0.0,0.0 regardless of where I stand. I'm not on the prime meridian, so what exactly is causing this problem?

I'm using this API from GitHub to access the GPS.

Does anyone know how I can fix these two problems?

Answer

If you are deploying it on Marshmallow , you need to additionally provide user permission request you can use this code before you actually call the location api , so that it has the proper access.

 if (Build.VERSION.SDK_INT >= 23) {

            if (ActivityCompat.checkSelfPermission(Login.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {  requestpermisions();``
            }
        }

public void requestpermisions() { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_CODE_1);

}

Do this for ACCESS_COARSE_LOCATION as well. and don't forget to turn on location services on your testing device.

Comments