android - 无法获取 Android 应用的位置权限

标签 android permissions gps location

我的 list 中有以下内容:

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

我还在手机设置中设置了位置权限。

是的,手机上启用了 GPS。

在这里,我检查权限:

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            Log.d(TAG, "location permission: " + hasLocationPermission); // 0

            if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
            hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            Log.d(TAG, "location permission: " + hasLocationPermission); // still 0
        }

如果我在我的 list 和手机的应用程序设置中获得许可,为什么它仍然阻止我访问该位置?因此,纬度和经度都返回 0.0。

最佳答案

声明你在哪里使用 Oncreate 或其他

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


    if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),this)) {
    //You fetch the Location here

    //code to use the 
    }
    else
    {
    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),this);
    }

    }

也在类里面声明

    public static void requestPermission(String strPermission,int perCode,Context _c,Activity _a){

    if (ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
        Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        } else {

        ActivityCompat.requestPermissions(_a,new String[]{strPermission},perCode);
        }
    }

public static boolean checkPermission(String strPermission,Context _c,Activity _a){
    int result = ContextCompat.checkSelfPermission(_c, strPermission);
    if (result == PackageManager.PERMISSION_GRANTED){

        return true;

    } else {

        return false;

    }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {

    case PERMISSION_REQUEST_CODE_LOCATION:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        fetchLocationData();

        } else {

        Toast.makeText(getApplicationContext(),"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();

        }
        break;

    }
}

对于其他较低的设备,您应该在声明时给予许可

关于android - 无法获取 Android 应用的位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38142097/

相关文章:

android - 在不同的应用程序中启动另一个 Activity 时需要帮助修复权限错误

Android - 谷歌地图 - LocationListener - 添加标记 onLocationChanged 使应用程序崩溃

java - 计算由纬度、经度表示的两点之间的距离,精度高达 15 英尺

android - libpng和Google Play的OpenALPR库问题

java - 应用程序的小部件、服务和模型(单例)之间的通信(类似 MVC 的架构)

Android 应用程序不需要任何特殊访问权限

SQL Server 数据屏蔽 屏蔽所需的最低权限

android - 融合的位置提供者似乎没有使用 GPS 接收器

androids putExtra

android - 使用 "JsonContentPolymorphicSerializer"时 Kotlinx 反序列化不包括来自抽象类的属性