当请求运行时位置许可时 [英] When asking for runtime permission for location

查看:114
本文介绍了当请求运行时位置许可时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,在首次启动我的应用程序时要求运行时权限时,它会提示用户使用其位置.而且,如果您单击是",则不会启用应有的位置.

Currently when asking for run time permission when launching my app for the very first time it prompts the user to use their location. And if you click yes it doesn't enable location like it should.

但是,如果我重新启动该应用程序,它将启用该位置.关于在第一次启动时可以在何处启用位置的任何建议?代码的第一部分在OnCreate中调用.

But if I relaunch the app it enables the location. Any suggestions as to where I can get it to have location enabled on first launch ? The first part of the code is called in OnCreate.

    if (ContextCompat.checkSelfPermission(MapsActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(MapsActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_CODE_ASK_PERMISSIONS);

        }
    }

  @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
        if (permissions.length == 1 &&
                permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            } else {
                // Show rationale and request permission.
            }
            mMap.setMyLocationEnabled(true);
        } else {
            //permission denied
        }
    }
}

  public void onMapReady(GoogleMap googleMap) {

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

推荐答案

如果您正在仿真器上进行测试,则仅在首次运行时才会询问权限. 如何调试Android 6.0权限?

If you are testing on an emulator the permission will only be asked on the first run. How to debug Android 6.0 permissions?

如果您还没有完全卸载该应用程序,并且正在从较低的SDK升级该应用程序,或者用户已在运行时设置了权限并将其设置为默认值,则不会再次询问运行时权限.

Also if you are not completely uninstalling the app and it is being upgraded from a lower SDK or permissions have been set by the user at runtime and made default, then the runtime permissions will not be asked again.

尝试完全卸载应用程序,或检查默认设置并清除它们.

Try completely uninstalling the app, or checking the default settings and clearing them.

这篇关于当请求运行时位置许可时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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