在清单中被授予ACCESS_FINE_LOCATION,但总是被拒绝吗? [英] ACCESS_FINE_LOCATION being granted in manifest, yet always getting permission denied?

查看:110
本文介绍了在清单中被授予ACCESS_FINE_LOCATION,但总是被拒绝吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试获取用户当前位置的纬度和经度.我已在清单中启用以下权限.TargetSDK和min sdk为23

I'm currently trying to get the latitude and longitude of the user's current location. I have enabled the following permissions in the manifest. TargetSDK and min sdk are 23

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false" />


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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainMenu" />
    <activity
        android:name=".NeedToKnow" />
    <activity
        android:name=".SelectTopic" />
    <activity
        android:name=".DisplayTopicInformation"/>
    <activity
        android:name=".SelectCheckList" />
    <activity
        android:name=".ViewCheckList"/>
    <activity
        android:name=".FindLocation"></activity>
</application>

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


</manifest>

为此,我了解我需要对权限进行运行时检查.但是,当我执行以下操作时,我总是得到-1的回报.(PERMISSION_DENIED)

In order to do so I understand that I need to perform a runtime check for the permissions. However when I perform the following I always get -1 in return. (PERMISSION_DENIED)

int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.ACCESS_FINE_LOCATION);

尝试访问当前位置时是否缺少我的步骤?

Is there a step I am missing when trying to access current locations?

完整代码在这里:

private String getZipcodeFromCurrentLocation() {

    int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.ACCESS_FINE_LOCATION);

    String zip = null;
    Location location = null;
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();

            Log.i("updated lat", String.valueOf(latitude));
            Log.i("updated lng", String.valueOf(longitude));
        }


    };

    try {
        if (PackageManager.PERMISSION_GRANTED == permissionCheck) {
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }

    } catch (SecurityException e){
        e.printStackTrace();
    }
    return zip;

}

推荐答案

如果用户已在手机设置中禁用了您的应用的位置权限,则您必须请求该权限.在下面,您将找到有关如何执行此操作的示例代码.

If user has disabled location permission for your app in the phone's settings, then you have to request that permission. Below you'll find a sample piece of code on how to do it.

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

public class MyActivity extends Activity implements  ActivityCompat.OnRequestPermissionsResultCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        if(permissionGranted) {
            // {Some Code}
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 200: {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // {Some Code}
                }
            }
        }
    }
}

我还建立了一个处理以上所有内容的类,并返回用户的位置.这是链接: https://www.dropbox.com/s/2mkyjok6mpna2yw/GPS.java?dl=0 .

Also I've build a class that handles all of the above and also returns user's location. Here's the link: https://www.dropbox.com/s/2mkyjok6mpna2yw/GPS.java?dl=0.

这篇关于在清单中被授予ACCESS_FINE_LOCATION,但总是被拒绝吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆