You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FusedLocationClient权限缺失:已添加ACCESS_COURSE_LOCATION仍提示需权限

Fixing the Persistent ACCESS_COARSE_LOCATION Permission Prompt

Hey there! I’ve dealt with this exact head-scratcher a few times when building location-based apps, so let’s break down the most likely reasons your app is still begging for the permission even after you added it to the XML, plus how to fix each one.

1. Double-check your permission placement in AndroidManifest.xml

First off, make sure you’ve added the permission outside the <application> tag, directly under the root <manifest> element. It’s super easy to accidentally nest it inside <application> where the system won’t recognize it at all.

Here’s the correct structure:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.your.package.name">

    <!-- Permission goes HERE, outside <application> -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <!-- Your activities, services, etc. go here -->
    </application>
</manifest>

2. You need to request the permission dynamically (Android 6.0+)

If your app targets API level 23 or higher (which most modern apps do), declaring permissions in the manifest isn’t enough. Dangerous permissions like location require runtime approval from the user—you can’t just rely on the XML alone.

Add this code to your Activity/Fragment where you initiate location access:

// Check if permission is already granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    // Request the permission from the user
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
            1001); // Use any integer code you like to identify this request
}

Don’t forget to handle the user’s response with this callback:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1001) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission approved—kick off your location logic here
        } else {
            // Permission denied—maybe show a message explaining why you need it to work properly
        }
    }
}

For Kotlin folks, here’s the equivalent:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(
        this,
        arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
        1001
    )
}

// Handle the permission result
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == 1001) {
        if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Proceed with fetching location
        } else {
            // Explain to the user why the permission is necessary
        }
    }
}

3. ProGuard might be stripping the permission

If you’re using code obfuscation with ProGuard, it sometimes removes permissions it thinks are unused—even if you actually need them! Add these lines to your proguard-rules.pro file to keep the location permission intact:

-keepattributes *Permission*
-keep class android.Manifest$Permission { *; }

4. Are you actually needing ACCESS_FINE_LOCATION instead?

Sometimes the system prompt can be a bit misleading. If your app uses precise location sources (like GPS via LocationManager.GPS_PROVIDER or FusedLocationProviderClient with high accuracy), you’ll need the ACCESS_FINE_LOCATION permission instead (or in addition to COARSE). Check your location logic—if you’re requesting precise location, add this to your manifest too:

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

And don’t forget to request it dynamically as well if targeting API 23+.

Start with the first check (manifest placement) since that’s the most common slip-up, then work your way through the others. Let me know if any of these fix your issue!

内容的提问来源于stack exchange,提问作者MasterG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:17:41