Android 6.0 multiple permissions

后端 未结 22 1852
太阳男子
太阳男子 2020-11-22 03:58

I know that Android 6.0 has new permissions and I know I can call them with something like this

if (ContextCompat.checkSelfPermission(this, Manifest.permiss         


        
22条回答
  •  孤街浪徒
    2020-11-22 04:16

    You can use Dexter

    In build.gradle add:

    implementation 'com.karumi:dexter:5.0.0'
    

    And use it in your activity as:

    val requiredPermissions = when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> listOf(Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION)
                else -> listOf(Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION)
            }
    
    Dexter.withActivity(this)
         .withPermissions(
               requiredPermissions
          )
         .withListener(object : MultiplePermissionsListener {
            override fun onPermissionRationaleShouldBeShown(
                 permissions: MutableList?,
                        token: PermissionToken?
                 ) {
                        /* ... */
                    }
    
    
            override fun onPermissionsChecked(report: MultiplePermissionsReport) = 
                 if (report.isAnyPermissionPermanentlyDenied) {
                    toast("You should grant all permissions") 
                 } else {
                    toast("All permissions granted")
    
                    // continue here if permission is a must
    
                  }).check()
    
                 // continue here if permission is not a must
    

提交回复
热议问题