[Android] Permission issues

One. Boat

  1. After the phone was upgraded to Android 10.0, the video suddenly couldn't be played. Later, Baidu found a solution

Just add this sentence in the application of the manifest android:requestLegacyExternalStorage="true"

  1. When importing a module in AS, there may be local modules that cannot be displayed when applying dependencies. At this time, you should check whether there is a corresponding app configuration in settings.gradle . Of course, the main app can only be configured with one
  2. Device File Explore to view the resource entry , the easiest way is to see if there is any on the right sidebar, usually there are
  3. The sending status of okhttp, such as the request header, which is written in Faraway, and the association between http and https
  4. Configure build is slow to download. In case one, access to google and jcenter may be blocked. Ali mirroring is used instead to prevent the outermost global build.gradle. Another method is to accelerate the portal
    repositories {
    
    
        maven{
    
     url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        google()
      /*  google()
        jcenter()*/
    }

    allprojects {
    
    
        repositories {
    
    
            maven{
    
     url 'http://maven.aliyun.com/nexus/content/groups/public/'}
            google()
            /*   google()
               jcenter()*/
        }
    }
  1. Android permissions
    1) Implementation concept :
    a. Permissions are first defined to a static character array
    b. Determine whether the current system is Android 6.0 (corresponding to API 23) and above, check the permission to write files (typically), if not, apply dynamically, three
    Three parameters (current class, permission set, request code) c...callback function, callback onRequestPermissionResult function after applying for permission, also three parameters

2. Optimization and upgrade

General permissions can be registered in the manifest file, but dangerous permissions must be dynamically applied for

  1. Part of the extraction method of loading data in initdata-"loadsongs(), dynamic application before calling data query
  2. Encapsulate another method (handlePermission) to deal with permission issues, and it’s pretty good
    Insert picture description here
  3. The specific upgrade idea is to click on the list interface to pop up the permission request. If you click to cancel, the next time you go to this interface, there will be an easy-to-understand upgrade. There are three steps to request permissions .
    1) in
    a. android.Manifest.permission.READ_EXTERNAL_STORAGEis a read packaged in the Manifest under the SDK . Take the attribute abbreviation, the first step is to check whether there is permission,
    b. The ActivityCompat.checkSelfPermission(it,permission)method is to check the permission, the first parameter is the context uses a lambda expression, the return is respectively, corresponding to true and false
    Insert picture description here
    c. The second method, check if it returns If the value is true, it means that the user did not understand, and the program needs to pop up a custom reminder to apply
 //处理权限问题
    private fun handlePermission() {
    
    
        val permission=android.Manifest.permission.READ_EXTERNAL_STORAGE
        val checkSelfPermission=context?.let {
    
     ActivityCompat.checkSelfPermission(it,permission) }
        if (checkSelfPermission==PackageManager.PERMISSION_GRANTED){
    
    
            //已经获取
            loadSongs()
        }else{
    
    
            //没有获取权限
            if(activity?.let {
    
     ActivityCompat.shouldShowRequestPermissionRationale(it,permission) }!!){
    
    
                //结果为true则需要弹出对话框进行再次请求
                //AlertDialog  anko封装
                alert("我们只会访问音乐文件,不会访问隐私照片","温馨提示") {
    
    
                    yesButton {
    
     myRequsetPerssion() }
                    noButton {
    
      }
                }.show()
            }else{
    
    
                //不需要弹出
                myRequsetPerssion()
            }
        }
    }

2) core: define the permission application method

private fun myRequsetPerssion() {
    
    
        //真正申请权限
        val permissions= arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE)//可以申请多个权限
        requestPermissions(permissions,1)
    }

3) Outjudge request result callback

/*
* 接收权限授权结果
* requestCode请求码
* permission权限申请数组
* grantResules申请之后结果//int array 只申请了一个就取首地址
* */
    //结果回调
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    
    
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
    
    
        loadSongs()
    }
    }

Guess you like

Origin blog.csdn.net/qq_38304672/article/details/105647969