简体   繁体   中英

Android Studio location permission denied

Hello Guys I'm facing "gps" location provider requires ACCESS_FINE_LOCATION permission. Although I included the libraries and also added all the required permissions to Manifest file. when I run the code it is giving me the permission error.

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.

        }

        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.dal.cs.csci3130.quickcash">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name=".User.SignupActivity"></activity>
        <activity android:name=".User.LoginActivity"></activity>
        <activity android:name=".Home.EmployeeDashboardActivity"></activity>
        <activity android:name=".Home.EmployeeHomeActivity"></activity>
        <activity android:name=".Home.EmployerHomeActivity"></activity>
        <activity android:name=".Home.ManualSearchActivity"></activity>
        <activity android:name=".Home.PreferencesActivity"></activity>
        <activity android:name=".Home.PreferenceSearchActivity"></activity>
        <activity android:name=".Home.ProfileActivity"></activity>
        <activity android:name=".Home.TaskDescriptionActivity"></activity>
        <activity android:name=".Home.EmployerCreateJobActivity"></activity>


        <activity
            android:name=".MainActivity"

            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Starting from Android 6 you need to request permission at runtime

example:

ActivityCompat.requestPermissions(MainActivity.this, new String[] { permission }, requestCode);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM