Android运行时权限-如何实现 [英] Android runtime permissions- how to implement

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

问题描述

Android 开发者文档 给出了在运行时请求权限的示例:

Android Developer Documentation gives this example of requesting permissions at runtime:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // 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(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

这个例子中的MY_PERMISSIONS_REQUEST_READ_CONTACTS"是什么?它说它是一个应用程序定义的 int 常量,但这是否意味着我应该创建一个 Constants.java 并声明一个公共静态 int?值应该是多少?

What is "MY_PERMISSIONS_REQUEST_READ_CONTACTS" in this example? It says it's an app-defined int constant, but does that mean I should make a Constants.java and declare a public static int? What should the value be?

在其他示例中,我看到人们在这里使用 1,或者 0 或 0xFFEEDDCC,但我找不到它是什么的解释.有人可以向我解释什么需要去这里以及为什么?(就我而言,我需要确保该应用有权访问精确位置)

In other examples I see people use 1 here, or 0 or 0xFFEEDDCC, but I can't find an explanation of what it is. Can someone explain to me what needs to go here and why? (In my case, I need to make sure the app has permission to access fine location)

ActivityCompat 文档说应用程序特定的请求代码与报告给 onRequestPermissionsResult 的结果相匹配"?这对我没有帮助.

The ActivityCompat documentation says "Application specific request code to match with a result reported to onRequestPermissionsResult"? This does not help me.

推荐答案

这个例子中的MY_PERMISSIONS_REQUEST_READ_CONTACTS"是什么?

What is "MY_PERMISSIONS_REQUEST_READ_CONTACTS" in this example?

它是一个 int,将特定的 requestPermissions() 调用绑定到相应的 onRequestPermissionsResult() 回调.

It is an int, to tie a particular requestPermissions() call to the corresponding onRequestPermissionsResult() callback.

在幕后,requestPermissions() 使用 startActivityForResult();这个 int 的作用与它在 startActivityForResult() 中的作用相同.

Under the covers, requestPermissions() uses startActivityForResult(); this int serves the same role as it does in startActivityForResult().

这是否意味着我应该创建一个 Constants.java 并声明一个 public static int?

does that mean I should make a Constants.java and declare a public static int?

我只想让它成为活动中的 private static final int.但是,您可以在任何地方声明它.

I would just make it a private static final int in the activity. But, you can declare it wherever you want.

价值应该是多少?

我似乎记得它需要低于 0x8000000,否则它可以是任何你想要的.您在活动中为每个 requestPermissions() 调用使用的值应该获得不同的 int,但实际数字并不重要.

I seem to recall that it needs to be below 0x8000000, but otherwise it can be whatever you want. The value that you use for each requestPermissions() call in an activity should get a distinct int, but the actual numbers do not matter.

如果您的活动只有一个 requestPermissions() 调用,那么 int 值真的无关紧要.但是许多应用程序在一个活动中会有多个 requestPermissions() 调用.在这种情况下,开发人员可能需要在 onRequestPermissionsResult() 中知道这是针对什么请求的结果.

If your activity has only one requestPermissions() call, then the int value really does not matter. But many apps will have several requestPermissions() calls in an activity. In that case, the developer may need to know, in onRequestPermissionsResult(), what request this is the result for.

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

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