写点什么

Android 6,android 网络开发技术实战详解

用户头像
Android架构
关注
发布于: 2 小时前

<span class="line" style="height:20px"> versionName "</span><span class="number" style="color:rgb(245,135,31)">1</span>.<span class="number" style="color:rgb(245,135,31)">0</span><span class="string" style="color:rgb(113,140,0)">"</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px"> minifyEnabled false</span>


<span class="line" style="height:20px"> proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px">}</span></span>


</pre></td></tr></tbody></table>


  • 检查并申请权限


我们需要在用到权限的地方,每次都检查是否 APP 已经拥有权限,


比如我们有一个下载功能,需要写 SD 卡的权限,


我们在写入之前检查是否有 WRITE_EXTERNAL_STORAGE 权限,没有则申请权限


<table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px"><tbody><tr style="background-color:rgb(249,249,249)"><td class="code" style="padding:0px; vertical-align:middle; border:none"><pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span class="line" style="height:20px"><span class="keyword" style="color:rgb(137,89,168)">if</span> (ContextCompat.checkSelfPermission(<span class="keyword" style="color:rgb(137,89,168)">this</span>, Manifest.permission.WRITE_EXTERNAL_STORAGE)</span>


<span class="line" style="height:20px"> <span class="comment" style="color:rgb(142,144,140)">//申请 WRITE_EXTERNAL_STORAGE 权限</span></span>


<span class="line" style="height:20px"> ActivityCompat.requestPermissions(<span class="keyword" style="color:rgb(137,89,168)">this</span>, <span class="keyword" style="color:rgb(137,89,168)">new</span> <span class="built_in" style="color:rgb(245,135,31)">String</span>[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},</span>


<span class="line" style="height:20px"> WRITE_EXTERNAL_STORAGE_REQUEST_CODE);</span>


<span class="line" style="height:20px">}</span>


</pre></td></tr></tbody></table>


  • 请求权限后,系统会弹出请求权限的 Dialog



  • 用户选择允许或拒绝后,会回调 onRequestPermissionsResult 方法, 该方法类似于 onActivityResult


<table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px"><tbody><tr style="background-color:rgb(249,249,249)"><td class="code" style="padding:0px; vertical-align:middle; border:none"><pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span class="line" style="height:20px"><span class="annotation">@Override</span></span>


<span class="line" style="height:20px"> <span class="keyword" style="color:rgb(137,89,168)">super</span>.onRequestPermissionsResult(requestCode, permissions, grantResults);</span>


<span class="line" style="height:20px"> doNext(requestCode,grantResults);</span>


<span class="line" style="height:20px">}</span>


</pre></td></tr></tbody></table>


  • 我们接着需要根据 requestCode 和 grantResults(授权结果)做相应的后续处理


<span class="line" style="height:20px"> <span class="comment" style="color:rgb(142,144,140)">// Permission Granted</span></span>


<span class="line" style="height:20px"> <span class="comment" style="color:rgb(142,144,140)">// Permission Denied</span></span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px"> }</span>


</pre></td></tr></tbody></table>


Fragment 中运行时权限的特殊处理




  • 在 Fragment 中申请权限,不要使用 ActivityCompat.requestPermissions, 直接使用 Fragment 的 requestPermissions 方法,否则会回调到 Activity 的 onRequestPermissionsResult

  • 如果在 Fragment 中嵌套 Fragment,在子 Fragment 中使用 requestPermissions 方法,onRequestPermissionsResult 不会回调回来,建议使用 getParentFragment().requestPermissions 方法,


这个方法会回调到父 Fragment 中的 onRequestPermissionsResult,加入以下代码可以把回调透传到子 Fragment


<table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px"><tbody><tr style="background-color:rgb(249,249,249)"><td class="code" style="padding:0px; vertical-align:middle; border:none"><pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span class="line" style="height:20px"><span class="annotation">@Override</span></span>


<span class="line" style="height:20px"> <span class="keyword" style="color:rgb(137,89,168)">super</span>.onRequestPermissionsResult(requestCode, permissions, grantResults);</span>


<span class="line" style="height:20px"> List<Fragment> fragments = getChildFragmentManager().getFragments();</span>


<span class="line" style="height:20px"> fragment.onRequestPermissionsResult(requestCode,permissions,grantResults);</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px"> }</span>


<span class="line" style="height:20px">}</span>


</pre></td></tr></tbody></table>


相关开源项目





使用标注的方式,动态生成类处理运行时权限,目前还不支持嵌套 Fragment。



基于 RxJava 的运行时权限检测框架



简化运行时权限的处理,比较灵活



Google 官方的例子


附录


--


以下权限只需要在 AndroidManifest.xml 中声明即可使用


<table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px"><tbody><tr style="background-color:rgb(249,249,249)"><td class="code" style="padding:0px; vertical-align:middle; border:none"><pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.ACCESS_LOCATION_EXTRA_COMMANDS</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.ACCESS_NETWORK_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.ACCESS_NOTIFICATION_POLICY</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.ACCESS_WIFI_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.ACCESS_WIMAX_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.BLUETOOTH</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.BLUETOOTH_ADMIN</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.BROADCAST_STICKY</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.CHANGE_NETWORK_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.CHANGE_WIFI_MULTICAST_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.CHANGE_WIFI_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.CHANGE_WIMAX_STATE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="cl


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ass">.DISABLE_KEYGUARD</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.EXPAND_STATUS_BAR</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.FLASHLIGHT</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.GET_ACCOUNTS</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.GET_PACKAGE_SIZE</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.INTERNET</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.KILL_BACKGROUND_PROCESSES</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.MODIFY_AUDIO_SETTINGS</span></span>


<span class="line" style="height:20px">android<span class="class">.permission</span><span class="class">.NFC</span></span>

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android 6,android网络开发技术实战详解