Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 407b3db

Browse files
committed
Merge pull request hehonghui#592 from shenyansycn/master
Master
2 parents 96f245c + f3f1716 commit 407b3db

File tree

3 files changed

+0
-78
lines changed

3 files changed

+0
-78
lines changed

issue-39/权限 - 第三篇.md

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@
99
* 校对者:
1010
* 状态 : 翻译中
1111

12-
With Marshmallow a new permissions model was added to Android which requires developers to take a somewhat different approach to permissions on Android. In this series we’ll take a look at ways to handle requesting permissions both from a technical perspective, and in term of how to provide a smooth user experience.
13-
1412
在Marshmallow(棉花糖,Android6.0版本)中Android添加了一个新的权限模块,需要开发者在授权的时候做一些不同的处理。在这一系列中,我们从技术角度看下如何处理请求的权限和如何提供流畅的用户体验。
1513

1614
![Icon_no_permission](https://i0.wp.com/blog.stylingandroid.com/wp-content/uploads/2015/12/Icon_no_permission.png?w=240)
1715

18-
Previously we looked at how we can incorporate the logic for checking that we have the required permissions, but next we’ll look at how we can actually request the permissions that haven’t been granted.
19-
2016
我们已经知道了如何检测我们需要的权限,现在我们看下如何请求被拒绝的权限。
2117

22-
The happy flow (for us as developers, at least) for this is that we ask the user to explicitly grant us a required permission, the user grantes it and everyone is happy. But, as we discussed previously, we need to cover the cases where the user denies us the permission we’ve requested.
23-
2418
令人高兴的流程是(至少对于开发者来说),我们直接询问用户授权需要的权限,用户授权了,大家都高兴。但是,就像我们之前讨论过的,我们需要考虑到用户没有授予权限的情况。
2519

26-
Let’s first look at the logic for the happy path as this is relatively straightforward:
27-
2820
现在让我们看下,我们直接提出请求的操作:
2921

3022
PermissionsActivity.java
@@ -87,12 +79,8 @@ public class PermissionsActivity extends AppCompatActivity {
8779
}
8880
```
8981

90-
We have a startActivityForResult() method which is a simple utility method which other Activities must use to start the PermissionsActivity with a given set of required permissions. So the flow is pretty straightforward once we have the required permissions. However, you may be wondering why we need this – surely we launched the Activity because we already determined that we don’t have the required permissions? The reason that we need this is for some of the following edge cases – if the user leaves this Activity temporarily, and grants the permission in Settings, then returns then this flow will be followed.
91-
9282
我们有一个非常实用的startActivityForResult()方法,其他Activity必须使用这个传入需要请求的一套权限来启动PermissionsActivity。所以我们可以直接一次都请求所需要的权限。然而,你可能会疑惑我们为什么需要这个-我们启动Activity是因为我们已经知道我们没有获得所需要的权限么?我们需要这个的原因是如下的这些小概率情况-如果用户临时离开了这个Activity,在设置中授权了权限,然后再回来的时候这个流程会次执行。
9383

94-
But what about when we don’t have the required permissions – we’ll need to request them:
95-
9684
但是当我们没有所需要的权限时怎么办-我们需要请求它们:
9785

9886
PermissionsActivity.java
@@ -130,16 +118,10 @@ public class PermissionsActivity extends AppCompatActivity {
130118
.
131119
```
132120

133-
This is the core of the process. We call requestPermissions() which passes control to the OS to request the permissions that we require. I always make a point of passing both normal and dangerous level permissions here even though we get automatically granted normal permissions. The reasoning behind this is that if what is now considered a normal permission was changed to a dangerous permission in the future then everything would still work.
134-
135121
这就是处理核心。我们调用requestPermissions()请求我们需要的权限。我这里总是强调标准和危险级别的权限,虽然我们自动获得标准的权限。背后的原因是,如果标准权限在未来变为一个危险的权限,那么仍会正常工作。
136122

137-
requestPermissions() operates in a similar manner to startActivityForResult() – we pass control to another Activity and then get a callback once that Activity completes. In this case the callback method is onRequestPermissionResult(). This checks that all of the requested permissions have been granted and either passes control back (either through finish() or invoking MainActivity, as before), or things become a little more complex: We have requested the required permission, and the user has denied it.
138-
139123
requestPermissions()的操作和startActivityForResult()的操作很像 - 我们在另一个Activity中处理,然后在本Activity中用一个回调做为结束。这个例子中的回调函数是onRequestPermissionResult()。它检测了所有被授权的权限和要么返回处理(要么通过finish(),要么重新唤醒MainActivity,就像之前那样),要么事情变为稍有复杂:我们请求所需要的权限但用户拒绝了。
140124

141-
The first time we ask the user for a particular permission following installation the user will be given with a simple choice: Allow or deny. If they deny us permission and we request it again they will also have a checkbox labelled “Never ask again”. If the user checks this and taps “Deny” then any subsequent requests that we make for the same permission will automatically be denied. Unfortunately we have no way of knowing if this has happened we will just get a PERMISSION_DENIED response. Bearing in mind that this particular permission is critical to the operation of this app we need to further inform the user of why this permission is required and, if they still refuse to grant it, exit the app:
142-
143125
首先,安装时我们会问用户一个特殊的权限,用户可以选择:允许或拒绝。如果拒绝了权限,我们会在此请求,这时会有一个单选框“不再询问”。如果用户选择了一个单选框并按了“拒绝”,之后的任何权限请求都会被自动的拒绝。不幸的是,我们的权限被拒绝了,我们并不知道用户是否执行了上面的操作。考虑到这个特殊的权限是App运行所急需的,我们需要明确告诉用户为什么我们需要这个权限,如果仍然被拒绝,那就退出App:
144126

145127
PermissionsActivity.java
@@ -177,33 +159,20 @@ public class PermissionsActivity extends AppCompatActivity {
177159
}
178160
```
179161

180-
I have deliberately kept the actual text used here somewhat vague because every app will need to explain its precise reasons for needing the permission in question. Also, for the sake of clarity, I have no way of providing different information for different permissions as only one dangerous permission is being requested. However is a real app you will probably need to determine which permission is missing and provide appropriate information for each.
181-
182162
我故意让这里的说明文本说的有点含糊,因为每一个app都要解释需要这个权限的具体原因。同样,为了清晰明了,当只有一个危险权限被请求时,我没有办法没有针对不同权限提供不同的信息。然而,一个真实的app,你很可能需要确定哪一个权限缺失了和针对每一个缺失的提供一个恰当的描述信息。
183163

184-
So here we can see the flow when the user initially denies the permission, exits and subsequently grants the permission:
185-
186164
这里我们可以看到当用户最初拒绝了这个权限,退出和后来的权限的授予。
187165

188166
[video](https://youtu.be/0YBb_lmsyIM)
189167

190-
191-
If the user permanently denies permission (by checkingNever ask again”) the flow becomes a little more complex for the user because we cannot provide a link directly to the Permissions Settings page for our app so we need to provide some instructions to help the user:
192-
193168
如果用户永远拒绝了权限(通过“不再提醒”选择框)。接下来会稍微复杂,因为我们不能提供一个我们app权限设置页面的直接链接,所以我们需要给用户提供一些说明:
194169

195170
[video](https://youtu.be/gqFIJvMqIpQ)
196171

197-
It would be nice if we could alter the flows between the two kinds of denial. The first video shows that we have to tell the user to alter things through Settings even though we subsequently see that exit and re-launch prompts them once again to allow or deny. Unfortunately we have no way of knowing if the user has selected “Never ask again” so we have to work against the worst-case scenario.
198-
199172
需要注意的是是否我们改变了这两者之间的流程。第一个视频展示的是我们告知用户通过设置改变,即使我们随后看到了退出和重新启动后再次提示的允许或拒绝。不幸的,我们没有办法知道用户是否选择了“不再提示”,所以我们要做最坏情况的打算。
200173

201-
That said we now have a relatively simple, reusable method of requesting the critical permissions for our app. In the final article in this series we’ll look at non-critical permissions and consider some best practise.
202-
203174
即便如此我们现在有一个相对简单,可以重复使用的请求我们应用关键权限的方法。在这个系列的最后一篇文章我们来看看非关键权限和更多的练习。
204175

205-
The source code for this article is available [here](https://github.com/StylingAndroid/Permissions/tree/Part3).
206-
207176
源代码在[这里](https://github.com/StylingAndroid/Permissions/tree/Part3)。
208177

209178
© 2016, [Mark Allison](https://blog.stylingandroid.com/). All rights reserved.

issue-39/权限 - 第二篇.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,29 @@
99
* 校对者:
1010
* 状态 : 翻译中
1111

12-
With Marshmallow a new permissions model was added to Android which requires developers to take a somewhat different approach to permissions on Android. In this series we’ll take a look at ways to handle requesting permissions both from a technical perspective, and in term of how to provide a smooth user experience.
13-
1412
在Marshmallow(棉花糖,Android6.0版本)中Android添加了一个新的权限模块,需要开发者在授权的时候做一些不同的处理。在这一系列中,我们从技术角度看下如何处理请求的权限和如何提供流畅的用户体验。
1513

1614
![Icon_no_permission](https://i0.wp.com/blog.stylingandroid.com/wp-content/uploads/2015/12/Icon_no_permission.png?w=240)
1715

18-
Previously we looked at how we could check whether we had already been granted our required permissions, but we had no mechanism in place to request any missing permissions. In this article we’ll look at how we can include the necessary permissions checking and requesting in all of our Activities without having large amounts of duplicate code.. Please bear in mind that everything that follows is specific to Marshmallow and later (earlier OS levels have permissions granted implicitly from the Manifest declarations) but that you’ll need to implement this kind of checking if you are specifying targetSdkVersion=23 or higher in your builds.
19-
2016
之前我们讨论了如何检查我们需要的权限是否被授予,但是我们没有讨论在适当的地方请求缺失的权限。在这篇文章中,我们来看一看在所有Acitivity中,在没有很多重复代码的情况下我们是如何请求和检测必要权限的。请记住,接下来的都是在Marshmallow或之后的版本(更早OS层Manifest声明的权限会被直接授权),如果你在builds文件中特别标明targetSdkVersion=23或更高,你就需要实现这种检测。
2117

22-
So the first thing we need to understand is how the permissions request model works. As we have already discussed, normal permissions will be granted implicitly but dangerous permissions will require the user to explicitly grant that permission. Things are easy enough when the user grants us the required permission but we need to defend against instances where the user denies us permission. For the app we’re going to eventually be developing it may not be obvious to the user why we require the RECORD_AUDIO permission, so we need to make some provision to inform the user of why this will be needed.
23-
2418
所以第一件事情是我们要知道权限请求模块是如何工作的。就像我们已经讨论过的,标准权限会被直接授权,但是危险的权限的请求需要用户授予。用户授予需要的权限是很容易的事情,但我们需要考虑到用户没有授予权限。针对这个app,我们最终编写成一个对用户来说并不是非常明显的我们需要RECORD_AUDIO权限的原因,所以,我们要准备通知用户为什么这个是需要的。
2519

26-
From the users’ perspective, the way this works is that the user will be asked for the require permission the first time they run the app:
27-
2820
从用户的角度来看,工作方式是第一次运行app的时候请求用户授权需要的权限:
2921

3022
![Part2-first-run](https://i1.wp.com/blog.stylingandroid.com/wp-content/uploads/2015/12/Part2-first-run.png?resize=624%2C468)
3123

32-
If they allow the permission then everything is fine and we can carry on. However, if they deny the permission then we can repeatedly ask them for the required permission:
33-
3424
如果他们授权了这个权限,万事好说,我们就不急了。但是如果他们拒绝了这个权限,我们就要反复的询问需要被授权的权限:
3525

3626
![Part2-second-run](https://i1.wp.com/blog.stylingandroid.com/wp-content/uploads/2015/12/Part2-second-run.png?resize=624%2C468)
3727

38-
But note that if the user has previously denied the permission being requested they will be given the option to never be asked for the permission again. If the user selects this option then any further attempts by our code to request the permission will automatically be denied without prompting the user. Clearly this can be problematic for us as developers, so we need to make allowance for it.
39-
4028
请注意如果用户之前拒绝了权限申请,它们会显示一个不再显示这个权限请求的选项。如果用户选择了一个选项,代码中任何进一步的尝试权限请求会被自动拒绝。做为开发者要清楚这个问题,做好预留措施。
4129

42-
This can further be compounded because the user can, at any time, go in to the Settings page for our app and grant or deny any permissions required by our app. This is why it is important to check that required permissions have been granted not only at app startup, but also in each Activity as the permissions could potentially change at any time.
43-
4430
用户可以进一步设置,进到应用的设置页面,可以授权或拒绝任何应用需要的权限。这就是为什么不仅仅在应用启动的时候检测需要的权限是否被授予的重要性,而且每个activity的权限都随时都有可能发生变化。
4531

46-
So the pattern that we’re going to use to manage this is to have a separate Activity which is responsible for requesting permissions, and all of the other Activities within our app will need to check that they have the permissions they require, and pass control back to the PermissionsActivity if they have not been granted.
4732

4833
我们将要管理这个的模式是一个单独的负责请求权限的Activity,app中所有的其他Activity都需要检测所需要的权限,如果他们没有被授权PermissionsActivity会被回调。
4934

50-
So let’s update our *MainActivity* slightly:
51-
5235
让我稍微更新下*MainActivity*
5336

5437
MainActivity.java
@@ -85,24 +68,14 @@ public class MainActivity extends AppCompatActivity {
8568
}
8669
```
8770

88-
We’ve moved the actual permissions check to onResume(). This is to cover the case where the user might pause our app, switch to Settings, deny a permission, and then resume our app. OK this is something of an edge case but it’s worth defending against crashes which could occur from this.
89-
9071
我们把权限检测代码放到`onResume()`。这就解决了用户暂停我们的app,切换到设置页面,拒绝了权限,然后又返回到我们的app的情况。好,这只是一个小概率事件,但对于预防崩溃是值得做的。
9172

92-
So the basic pattern that we’re implementing here is that whenever our Activity is resumed we confirm that we have the required permissions for that Activity to operate. If we don’t then we pass control to the PermissionsActivity which is responsible for obtaining the required permissions. Although this feels like a really defensive approach I feel that it really is a sensible approach which doesn’t actually require an awful lot of code. All of the checking logic in encapsulated within PermissionsChecker, and the request logic is handled in PermissionsActivity.
93-
9473
我们实现的这个基础模式是无论何时Activity被恢复我们都可以确定有操作Activity的所需要的权限。如果我们没有权限,我们将控制权交给负责获得需要的权限的PermissionsActivity。虽说这感觉像是一个防御性方案,但我认为这个一个不需要大量代码的明智的方案。PermissionsChecker内封装好了所有检测逻辑,请求调用是在PermissionsActivity。
9574

96-
Having the permissions check as a relatively lightweight component is important because we can check for required permissions relatively cheaply and only make the rather more expensive Activity change to request missing permissions where absolutely necessary.
97-
9875
轻量级的权限检测组件是重要的,因为我们可以相对简单的检测需要的权限,在Activity有必要请求权限的地方请求是更浪费的。
9976

100-
In the next article we’ll take a look at PermissionsActivity to see how we actually handle the permission requests and explore how to further inform the user why a permission is required if they deny a permission that we’re requested.
101-
10277
在下一篇文章中我们会看到PermissionsActivity如何处理这个权限的请求,探索如果用户拒绝了一个我们需要的权限,如何告知用户我们为什么需要。
10378

104-
The source code for this article is available [here](https://github.com/StylingAndroid/Permissions/tree/Part2). There’s a placeholder PermissionsActivity in the code which we’ll expand upon in the next article, so it’s not fully functional code, I’m afraid.
105-
10679
这篇文章的源码在[这里](https://github.com/StylingAndroid/Permissions/tree/Part2)。PermissionsActivity代码中有一个占位符,我会在下篇文章中扩展,所以它现在不是一个完整功能的代码。
10780

10881
© 2016, [Mark Allison](https://blog.stylingandroid.com/). All rights reserved.

0 commit comments

Comments
 (0)