Android6.0 Dynamic permission application

Making the address

Apply for permission using the Fragment

Testing authority

  • Check the permission status with ContextCompat/ActivityCompat checkSelfPermission
  • CheckPermission status using the Context/Fragment checkPermission/checkSelfPermission (API 23)

To apply for permission

  • RequestPermissions via the Activity/Fragment requestPermissions (API 23)
  • RequestPermissions through ActivityCompat’s requestPermissions

Permission Application Result

  • By rewriting the Activity/fragments onRequestPermissionsResult function for application result grantResults return values for the PackageManager. All permissions through PERMISSION_GRANTED

Use after encapsulation

// Request permission
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        this.apply(Manifest.permission.READ_EXTERNAL_STORAGE) { reject, forever ->
            forever.forEach {
                println("Permission to permanently refuse:$it")}if (reject.isEmpty()) {
                println("ok")}else {
                println("no")}}}}Copy the code
// Apply for permission in batches
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        this.apply(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { reject, forever ->
            forever.forEach {
                println("Permission to permanently refuse:$it")}if (reject.isEmpty()) {
                println("ok")}else {
                println("no")}}}}Copy the code
// Apply for the apK installation permission
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        this.applyInstallPackage { reject, _ ->
            if (reject.isEmpty()) {
                println("ok")}else {
                println("no")}}}}Copy the code
// Open the APP Settings page
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        this.openAppSettings()

    }

}
Copy the code