• easypermissions的基本使用


    转载请注明出处 http://blog.csdn.net/pyfysf/article/details/78204395

    Android 6.0(API 级别 23)开始,在应用运行时向其授予权限,而不是在应用安装时授予。这样可以让用户对应用的功能进行更多控制;

    系统权限分为两类:正常权限和危险权限:


    正常权限不会直接给用户隐私权带来风险。如果你的应用在其清单中列出了正常权限,系统将自动授予该权限。


    危险权限会授予应用访问用户隐私数据的权限。如果你列出了危险权限,则用户必须明确批准您的应用使用这些权限。


    注:从 Android 6.0(API 级别 23)开始,用户可以随时从任意应用调用权限,即使应用面向较低的 API 级别也可以调用。无论您的应用面向哪个 API 级别,您都应对应用进行测试,以验证它在缺少需要的权限时行为是否正常。

    • 正常权限
    NFC  
    READ_SYNC_SETTINGS  
    READ_SYNC_STATS  
    RECEIVE_BOOT_COMPLETED  
    REORDER_TASKS  
    REQUEST_INSTALL_PACKAGES  
    SET_ALARM  
    SET_TIME_ZONE  
    SET_WALLPAPER  
    SET_WALLPAPER_HINTS  
    TRANSMIT_IR  
    UNINSTALL_SHORTCUT  
    USE_FINGERPRINT  
    VIBRATE  
    WAKE_LOCK  
    WRITE_SYNC_SETTINGS 
    ACCESS_LOCATION_EXTRA_COMMANDS  
    ACCESS_NETWORK_STATE  
    ACCESS_NOTIFICATION_POLICY  
    ACCESS_WIFI_STATE  
    BLUETOOTH  
    BLUETOOTH_ADMIN  
    BROADCAST_STICKY  
    CHANGE_NETWORK_STATE  
    CHANGE_WIFI_MULTICAST_STATE  
    CHANGE_WIFI_STATE  
    DISABLE_KEYGUARD  
    EXPAND_STATUS_BAR  
    GET_PACKAGE_SIZE  
    INSTALL_SHORTCUT  
    INTERNET  
    KILL_BACKGROUND_PROCESSES  
    MODIFY_AUDIO_SETTINGS  
    
    • 危险权限
    group:android.permission-group.CONTACTS  
      permission:android.permission.WRITE_CONTACTS  
      permission:android.permission.GET_ACCOUNTS  
      permission:android.permission.READ_CONTACTS  
    
    group:android.permission-group.PHONE  
      permission:android.permission.READ_CALL_LOG  
      permission:android.permission.READ_PHONE_STATE  
      permission:android.permission.CALL_PHONE  
      permission:android.permission.WRITE_CALL_LOG  
      permission:android.permission.USE_SIP  
      permission:android.permission.PROCESS_OUTGOING_CALLS  
      permission:com.android.voicemail.permission.ADD_VOICEMAIL  
    
    group:android.permission-group.CALENDAR  
      permission:android.permission.READ_CALENDAR  
      permission:android.permission.WRITE_CALENDAR  
    
    group:android.permission-group.CAMERA  
      permission:android.permission.CAMERA  
    
    group:android.permission-group.SENSORS  
      permission:android.permission.BODY_SENSORS  
    
    group:android.permission-group.LOCATION  
      permission:android.permission.ACCESS_FINE_LOCATION  
      permission:android.permission.ACCESS_COARSE_LOCATION  
    
    group:android.permission-group.STORAGE  
      permission:android.permission.READ_EXTERNAL_STORAGE  
      permission:android.permission.WRITE_EXTERNAL_STORAGE  
    
    group:android.permission-group.MICROPHONE  
      permission:android.permission.RECORD_AUDIO  
    
    group:android.permission-group.SMS  
      permission:android.permission.READ_SMS  
      permission:android.permission.RECEIVE_WAP_PUSH  
      permission:android.permission.RECEIVE_MMS  
      permission:android.permission.RECEIVE_SMS  
      permission:android.permission.SEND_SMS  
      permission:android.permission.READ_CELL_BROADCASTS 

    因此,如果API 23及以上时,我们申请危险权限时,除了在清单文件配置,还要在代码中进行动态申请

    我们可以自己去写代码进行权限代码申请,这种方法可以参考这样一篇博客

    Android6.0及以上版本申请权限讲解

    利用easypermissions进行权限申请

    首先在app项目下的build.gradle中添加

    android {
    ....
    }
    
    repositories {
        maven {
            url "http://maven.aliyun.com/nexus/content/repositories/releases"
        }
    }
    
    dependencies {
    ....
        compile 'pub.devrel:easypermissions:1.0.1'
    }
    

    这里以照相机权限和位置权限作为例子来讲解;
    首先在清单文件(AndroidManifest.xml)中声明这两个属性

    
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    然后在Activity中重写:

    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
            // Forward results to EasyPermissions
            EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        }

    然后我们就可以直接在Activity中书写请求权限的代码了

    //请求码
     @AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
    
        private void methodRequiresTwoPermission() {
            String[] perms = {Manifest.permission.CAMERA,
                    Manifest.permission.ACCESS_FINE_LOCATION};
            if (EasyPermissions.hasPermissions(this, perms)) {
                // Already have permission, do the thing
                // ...
    
                 Toast.makeText(this, "已经授权", Toast.LENGTH_SHORT).show();
                //表明已经授权,可以进行用户授予权限的操作
    
    
            } else {
                // Do not have permissions, request them now
    
    
    //弹出一个对话框进行提示用户
                EasyPermissions.requestPermissions(this, "应用时需要照相机权限",
                        RC_CAMERA_AND_LOCATION, perms);
    
            }
        }

    第一次打开app之后,拒绝权限。第二次进入app就会弹出这个对话框

    在oncreate方法中调用这个方法就可以了。

    完整的MainActivity

    
    public class MainActivity extends AppCompatActivity  {
    
        private static final int RC_CAMERA_AND_LOCATION = 100;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            methodRequiresTwoPermission();
        }
    
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
            // Forward results to EasyPermissions
            EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        }
    
    
        @AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
        private void methodRequiresTwoPermission() {
            String[] perms = {Manifest.permission.CAMERA,
                    Manifest.permission.ACCESS_FINE_LOCATION};
            if (EasyPermissions.hasPermissions(this, perms)) {
                // Already have permission, do the thing
                // ...
    
                 Toast.makeText(this, "已经授权", Toast.LENGTH_SHORT).show();
    
    
    
            } else {
                // Do not have permissions, request them now
    
                EasyPermissions.requestPermissions(this, "应用时需要照相机权限",
                        RC_CAMERA_AND_LOCATION, perms);
    
            }
        }
    
    
    }
    

    easypermissions的github地址,更多详细代码点击这里

  • 相关阅读:
    【RS】Automatic recommendation technology for learning resources with convolutional neural network
    卷积神经网络的入门
    Debug 路漫漫-10:AttributeError: 'Embedding' object has no attribute 'get_shape'
    Debug 路漫漫-09:构建CNN时维度不一致问题
    Debug 路漫漫-08:Keras 版本升级函数变换导致的问题
    springboot 过滤器、拦截器、消息转换器、切片执行顺序 及区别
    java InputStream读取数据问题
    Springboot 2-OAuth 2修改登录加密方式
    Spring Boot Security Oauth2之客户端模式及密码模式实现
    oauth2.0通过JdbcClientDetailsService从数据库读取相应的配置
  • 原文地址:https://www.cnblogs.com/shaofeer/p/11154415.html
Copyright © 2020-2023  润新知