未在特定片段中显示运行时权限对话框

时间:2017-05-18 11:41:05

标签: android android-fragments runtime-permissions

运行时权限对话框未显示在特定片段中。例如,有5个碎片。在One Fragment中有一张地图。当用户点击特定片段时,对话框应弹出给用户。

到目前为止,我所做的是: -

if (Prefs.getLatitude(getActivity()).equalsIgnoreCase("") && Prefs.getLongitude(getActivity()).equalsIgnoreCase("")) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mCheckPermissionP();
            }

mCheckPermissionP()包含以下代码: -

public void mCheckPermissionP() {
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    PERMISSION_REQUEST_FINE_LOCATION);

        }
    }

在onRequestPermissionresult()内部,代码如下: -

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case PERMISSION_REQUEST_FINE_LOCATION: {
                if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    initilizeMap();
                } else {
                    initilizeMap();
                }
            }
        }
    }

什么都没有被召唤。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但这是我的错
我忘记了在清单中定义权限 如果清单尝试定义这些权限时缺少权限,则不会出现“允许权限”对话框

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

在清单中