Android Location运行时权限 [英] Android Location runtime permissions

查看:172
本文介绍了Android Location运行时权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这类问题经常在堆栈溢出时发生.我看过很多有类似问题的帖子,阅读了官方文档,但最终我还是无法理解运行时权限如何在android中工作.

I know that this type of questions occurs very often on stack overflow. I have looked at many posts with similar questions, read the official documentation but finally I anyway cannot understand how do the runtime permissions work in android.

首先,我收到以下错误消息:

Firstly I get the following error:

Call requires permissions which may be rejected by user

当我生成Android Studio建议的代码时,我得到了:

When I generate the code that Android Studio suggests, I get this:

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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.
        return;
    }

我的第一个问题是:如果我只想使用精细位置,是否还需要检查粗略位置"权限?

My first question is: if I want to use only fine location, do I have anyway to check the Coarse Location permissions also?

接下来我该怎么办?我应该在生成的代码的括号中包括什么?我如何以及在何处请求权限?

What should I do next? What should I include in the brackets of the generated code? How and where do I request the permissions?

这是我的代码中必须包含权限的部分(mLastLocation行):

Here is the part of my code where I must include permissions (the mLastLocation line):

    private void displayLocation() {

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();

        lblLocation.setText(latitude + ", " + longitude);

    } else {

        lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
    }
}

在AndroidManifest中定义的权限:

Permission defined in AndroidManifest:

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

非常感谢!

推荐答案

精细位置:

精细位置提供了更好,更准确的位置.它允许同时使用GPS_PROVIDER和NETWORK_PROVIDER

The Fine location provides better and accurate locations.It gives permission for using both GPS_PROVIDER and NETWORK_PROVIDER

粗略位置:

粗略"位置提供的位置不太准确.它仅允许使用NETWORK_PROVIDER来确定位置.

The Coarse location provides less accurate locations.It gives permission for using both NETWORK_PROVIDER only for determining the position.

上下文:

上下文允许访问特定于应用程序的资源和类,以及对应用程序级操作(如启动活动,广播和接收意图等)的调用.

Context allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

您可以使用下面的类启用权限

You can Enable Permission Using the below class

UtilityLocation.Java

UtilityLocation.Java

public class UtilityLocation {
    public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static boolean checkPermission(final Context context)
    {
        int currentAPIVersion = Build.VERSION.SDK_INT;
        if(currentAPIVersion>= Build.VERSION_CODES.M)
        {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.ACCESS_FINE_LOCATION)) {
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                    alertBuilder.setCancelable(true);
                    alertBuilder.setTitle("Permission necessary");
                    alertBuilder.setMessage("Camera permission is necessary");
                    alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                        }
                    });
                    AlertDialog alert = alertBuilder.create();
                    alert.show();

                } else {
                    ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                }
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
}

MainActivity.Java

MainActivity.Java

检查权限是否已启用

Private Context mContext;
mContext=MainActivty.this;

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // only for gingerbread and newer versions
                boolean resultLocation = UtilityLocation.checkPermission(mContext);
                if (resultLocation) {
       //Allow Permission and Call your Location Activity.
             }
            } else {
      //Call your Location Activity
          }
        }

这篇关于Android Location运行时权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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