Android6.0 动态权限 笔录
EdwDoe
8年前
<h2><strong>导航:</strong></h2> <ul> <li>Android6.0 动态权限申请程序</li> <li>普通权限与动态权限</li> <li>完整代码</li> </ul> <p>在Android6.0中,系统权限被分为两大类:一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。</p> <h2><strong>一、Android6.0 动态权限申请程序</strong></h2> <p>当我们的Android工程的targetSdkVersion大于等于23的时候,对于Dangerous Permission,我们就需要手动编写逻辑代码实现权限的授权申请。</p> <p>以申请相机权限为例,首先,需要在配置文件AndroidManifest中申请我们需要的权限:</p> <pre> <code class="language-java"><!-- 相机 --> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /></code></pre> <p>第二步,我在需要使用相机权限的地方进行权限申请操作。首先判断当前系统是否为Android6.0或之上的版本,如果不是,直接直接执行下一步,否则进入权限申请程序:</p> <p>首先,checkSelfPermission检查该权限是否已经申请;</p> <p>shouldShowRequestPermissionRationale检查用户是否已经拒绝过一次了,再次弹出授权框询问并解释为什么需要这个权限;</p> <p>ActivityCompat.requestPermissions进行权限申请,申请的结果在onRequestPermissionsResult中接收。</p> <pre> <code class="language-java">//SDK版本为Android6.0以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ //检查权限 int checkPermissionCamera = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA); if(checkPermissionCamera != PackageManager.PERMISSION_GRANTED){ Log.d(Tag, "权限还没有申请"); if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)){ Log.d(Tag, "用户已经拒绝一次了,再次弹出授权框询问并解释为什么需要这个权限"); } else { //申请授权 Log.d(Tag, "申请授权"); ActivityCompat.requestPermissions(MainActivity.this //要申请的权限 , new String[] { Manifest.permission.CAMERA } //申请请求返回码 , 100); } } else{ Log.d(Tag, "权限已经申请"); } }</code></pre> <p>onRequestPermissionsResult:</p> <pre> <code class="language-java">@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){ switch (requestCode){ case 100:{ if(permissions[0].equals(Manifest.permission.CAMERA) && grantR ![1.png](http://upload-images.jianshu.io/upload_images/797252-49f05105989697ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)esults[0] == PackageManager.PERMISSION_GRANTED){ Log.d(Tag, "CAMERA 权限授权成功"); } else{ Log.d(Tag, "用户拒绝授权权限 CAMERA"); } break; } } super.onRequestPermissionsResult(requestCode, permissions, grantResults); }</code></pre> <p>各种情况下的程序运行流程:</p> <p>1、已经授权:</p> <p><img src="https://simg.open-open.com/show/94b696ced749144fa35b9fd5aa090b50.png"></p> <p>1.png</p> <p>2、未授权,授权框点击允许:</p> <p><img src="https://simg.open-open.com/show/c83f9de5cc66ab82c83a0c80f1cc2493.png"></p> <p>2.png</p> <p>3、未授权,授权框点击拒绝:</p> <p><img src="https://simg.open-open.com/show/85382eccbb0401ee857f8d8ef161700a.png"></p> <p>3.png</p> <h2><strong>二、普通权限与动态权限</strong></h2> <h2><strong>1、普通权限</strong></h2> <pre> <code class="language-java">android.permission.ACCESS_LOCATION_EXTRA_COMMANDS android.permission.ACCESS_NETWORK_STATE android.permission.ACCESS_NOTIFICATION_POLICY android.permission.ACCESS_WIFI_STATE android.permission.ACCESS_WIMAX_STATE android.permission.BLUETOOTH android.permission.BLUETOOTH_ADMIN android.permission.BROADCAST_STICKY android.permission.CHANGE_NETWORK_STATE android.permission.CHANGE_WIFI_MULTICAST_STATE android.permission.CHANGE_WIFI_STATE android.permission.CHANGE_WIMAX_STATE android.permission.DISABLE_KEYGUARD android.permission.EXPAND_STATUS_BAR android.permission.FLASHLIGHT android.permission.GET_ACCOUNTS android.permission.GET_PACKAGE_SIZE android.permission.INTERNET android.permission.KILL_BACKGROUND_PROCESSES android.permission.MODIFY_AUDIO_SETTINGS android.permission.NFC android.permission.READ_SYNC_SETTINGS android.permission.READ_SYNC_STATS android.permission.RECEIVE_BOOT_COMPLETED android.permission.REORDER_TASKS android.permission.REQUEST_INSTALL_PACKAGES android.permission.SET_TIME_ZONE android.permission.SET_WALLPAPER android.permission.SET_WALLPAPER_HINTS android.permission.SUBSCRIBED_FEEDS_READ android.permission.TRANSMIT_IR android.permission.USE_FINGERPRINT android.permission.VIBRATE android.permission.WAKE_LOCK android.permission.WRITE_SYNC_SETTINGS com.android.alarm.permission.SET_ALARM com.android.launcher.permission.INSTALL_SHORTCUT com.android.launcher.permission.UNINSTALL_SHORTCUT</code></pre> <h2><strong>2、危险权限</strong></h2> <p><img src="https://simg.open-open.com/show/61822a5f658b5256161e915f414759f5.png"></p> <h2><strong>三、完整代码</strong></h2> <pre> <code class="language-java">package com.peos.permissiondemo; import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String Tag = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((Button)findViewById(R.id.btn_signle)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //SDK版本为Android6.0以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ //检查权限 int checkPermissionCamera = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA); if(checkPermissionCamera != PackageManager.PERMISSION_GRANTED){ Log.d(Tag, "权限还没有申请"); if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)){ Log.d(Tag, "用户已经拒绝一次了,再次弹出授权框询问并解释为什么需要这个权限"); } else { //申请授权 Log.d(Tag, "申请授权"); ActivityCompat.requestPermissions(MainActivity.this //要申请的权限 , new String[] { Manifest.permission.CAMERA } //申请请求返回码 , 100); } } else{ Log.d(Tag, "权限已经申请"); } } } }); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){ switch (requestCode){ case 100:{ if(permissions[0].equals(Manifest.permission.CAMERA) && grantResults[0] == PackageManager.PERMISSION_GRANTED){ Log.d(Tag, "CAMERA 权限授权成功"); } else{ Log.d(Tag, "用户拒绝授权权限 CAMERA"); } break; } } super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }</code></pre> <p> </p> <p>来自:http://www.jianshu.com/p/390bd60e6ee3</p> <p> </p>