【问题标题】:Android Studio location permission deniedAndroid Studio 位置权限被拒绝
【发布时间】:2021-11-05 22:15:36
【问题描述】:

大家好,我面临“gps”位置提供者需要 ACCESS_FINE_LOCATION 权限。 虽然我包含了库并且还向清单文件添加了所有必需的权限。当我运行代码时,它给了我权限错误。

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

这是清单文件:

<?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>

【问题讨论】:

  • 你有一个评论的 TODO 你必须完成。
  • 您在运行时检查权限,但没有请求权限。因此,除非您在设置中手动授予它们,否则您没有它们。此外,您使用 getLastKnownLocation 会伤害自己。通常最后一个位置是未知的,所以它返回 null。如果需要位置,则应改为 requestSingleUpdate。

标签: java android


【解决方案1】:

从 Android 6 开始,您需要在运行时请求权限

示例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2016-09-15
    • 2013-07-03
    • 2015-12-21
    • 2020-06-03
    相关资源
    最近更新 更多