唐基歌德 發表於 2019-10-31 22:37:00

Android开发中权限配置

<p>  对于Android开发来说,经常需要申请权限,Android6.0以前,所有权限的配置只需要在配置文件AndroidManifest.xml中编写权限配置就可以了,但Android6.0以后版本中,Google为了提高系统的安全性,当APP需要用到用户隐私权限时需要进行权限配置,除了在配置文件AndroidManifest.xml中编写权限配置之外,还需要手动进行权限适配。申请权限的方式有以下两种:</p>
<p>  ①、在配置文件AndroidManifest.xml中编写权限配置</p>
<p>  ②、在逻辑代码中动态申请权限授权</p>
<p>  Android权限可以分为两大类:普通权限和高级权限。</p>
<p><strong>一、普通权限</strong></p>
<p>  常见的普通权限如下所示。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">ACCESS_LOCATION_EXTRA_COMMANDS   定位权限
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">ACCESS_NETWORK_STATE         网络状态权限
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">ACCESS_NOTIFICATION_POLICY      通知 APP通知显示在状态栏
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">ACCESS_WIFI_STATE WiFi         状态权限
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">BLUETOOTH                      使用蓝牙权限
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">BLUETOOTH_ADMIN             控制蓝牙开关
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">BROADCAST_STICKY            粘性广播
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">CHANGE_NETWORK_STATE         改变网络状态
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)">CHANGE_WIFI_MULTICAST_STATE   改变WiFi多播状态,应该是控制手机热点(猜测)
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">CHANGE_WIFI_STATE           控制WiFi开关,改变WiFi状态
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">DISABLE_KEYGUARD            改变键盘为不可用
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">EXPAND_STATUS_BAR            扩展bar的状态
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">GET_PACKAGE_SIZE            获取应用安装包大小
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">INTERNET                网络权限
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">KILL_BACKGROUND_PROCESSES      杀死后台进程
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">MODIFY_AUDIO_SETTINGS         改变音频输出设置
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">NFC                   支付
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)">READ_SYNC_SETTINGS           获取手机设置信息
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">READ_SYNC_STATS             数据统计
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">RECEIVE_BOOT_COMPLETED        监听启动广播
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">REORDER_TASKS             创建新栈
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)">REQUEST_INSTALL_PACKAGES       安装应用程序
</span><span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 0, 0, 1)">SET_TIME_ZONE            允许应用程序设置系统时间区域
</span><span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)">SET_WALLPAPER            设置壁纸
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 0, 1)">SET_WALLPAPER_HINTS          设置壁纸上的提示信息,个性化语言
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">TRANSMIT_IR               红外发射
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">USE_FINGERPRINT             指纹识别
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">VIBRATE                震动
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">WAKE_LOCK                 锁屏
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">WRITE_SYNC_SETTINGS        改变设置
</span><span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 0, 0, 1)">SET_ALARM                设置警告提示
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">INSTALL_SHORTCUT               创建快捷方式
</span><span style="color: rgba(0, 128, 128, 1)">33</span> UNINSTALL_SHORTCUT           删除快捷方式</pre>
</div>
<p>  在AndroidManifest.xml文件中的配置方式如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">//XXXX为需要配置的权限名称
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.XXXX"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span></pre>
</div>
<p><strong>二、高级权限</strong></p>
<p>  高级权限如下所示。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">READ_CALENDAR        日历
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">WRITE_CALENDAR        日历
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">CAMERA            相机
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">READ_CONTACTS         联系人
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">WRITE_CONTACTS      联系人
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">GET_ACCOUNTS         联系人
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">ACCESS_FINE_LOCATION    位置
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">ACCESS_COARSE_LOCATION位置
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)">RECORD_AUDIO         麦克风
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">READ_PHONE_STATE       电话
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">CALL_PHONE          电话
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">READ_CALL_LOG        电话
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">WRITE_CALL_LOG        电话
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">ADD_VOICEMAIL        电话
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">USE_SIP            电话
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">PROCESS_OUTGOING_CALLS电话
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">BODY_SENSORS         传感器
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)">SEND_SMS           短信
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">RECEIVE_SMS         短信
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">READ_SMS           短信
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">RECEIVE_WAP_PUSH      短信
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)">RECEIVE_MMS         短信
</span><span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 0, 0, 1)">READ_EXTERNAL_STORAGE存储
</span><span style="color: rgba(0, 128, 128, 1)">24</span> WRITE_EXTERNAL_STORAGE 存储</pre>
</div>
<p>  动态权限申请的逻辑代码如下所示(以官方读写内存为例):</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> requestAllPower() {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (ContextCompat.checkSelfPermission(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">                Manifest.permission.WRITE_EXTERNAL_STORAGE)
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>               !=<span style="color: rgba(0, 0, 0, 1)"> PackageManager.PERMISSION_GRANTED) {
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (ActivityCompat.shouldShowRequestPermissionRationale(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">                  Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>             } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>               ActivityCompat.requestPermissions(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>                         <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
</span><span style="color: rgba(0, 128, 128, 1)">10</span>                                 Manifest.permission.READ_EXTERNAL_STORAGE}, 1<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">13</span>   }</pre>
</div>
<p>  判断动态申请权限的结果代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">@Override
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onRequestPermissionsResult(<span style="color: rgba(0, 0, 255, 1)">int</span> requestCode, String[] permissions, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[] grantResults) {
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>         <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onRequestPermissionsResult(requestCode, permissions, grantResults);
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (requestCode == 1<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>             <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; permissions.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>               <span style="color: rgba(0, 0, 255, 1)">if</span> (grantResults ==<span style="color: rgba(0, 0, 0, 1)"> PERMISSION_GRANTED) {
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>                     Toast.makeText(<span style="color: rgba(0, 0, 255, 1)">this</span>, "" + "权限" + permissions + "申请成功"<span style="color: rgba(0, 0, 0, 1)">, Toast.LENGTH_SHORT).show();
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>               } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">10</span>                     Toast.makeText(<span style="color: rgba(0, 0, 255, 1)">this</span>, "" + "权限" + permissions + "申请失败"<span style="color: rgba(0, 0, 0, 1)">, Toast.LENGTH_SHORT).show();
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">                }
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">14</span>   }</pre>
</div>
<p>  对于动态权限申请,可以采用封装好的权限申请框架方便的处理权限申请问题,这样有利于代码的重用和整洁性,可以参考以下几个链接:</p>
<p>  ①、https://blog.csdn.net/fireqiang2/article/details/78343233</p>
<p>  ②、https://blog.csdn.net/wdd1324/article/details/79402573</p>
<p>  ③、https://blog.csdn.net/jsxin0816/article/details/78605731</p>
<p>  ④、https://www.jianshu.com/p/52e66d9e4a9a</p><br><br>
来源:https://www.cnblogs.com/chenfeifen/p/11774339.html
頁: [1]
查看完整版本: Android开发中权限配置