Follow Android 007, get a full set of Free Android development learning materials

What are run-time permissions

In order to better protect user privacy and security, Android since version 6.0, the introduction of runtime dynamic permission check mechanism. When an application needs to access some user privacy and security permissions, the application needs to invoke the system permission check interface to check whether the permissions are obtained. If no permission application window is displayed, the user can decide whether to allow the application.

Based on the sample

The following example will apply for call permission and complete the call function.

Apply for permission in android native mode

class MainActivity : AppCompatActivity() {

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

    /** * First check permission, if you have permission, call directly; * Otherwise, apply for permission. * /
    private fun applyPermissionAndCall(a) {
        if (hasPermission()) {
            call()
        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE), 1)}}/** * Check whether there are permissions */
    private fun hasPermission(a): Boolean {
        val result = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
        return result == PackageManager.PERMISSION_GRANTED
    }

    /** * Process the result of the permission application. If the permission is obtained, call the phone number; otherwise, TOAST will report an error. * /
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            1- > {if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    call()
                } else {
                    toast("No permission, can't call 😢.")}}}}/** * call system call function, dial 10086 */
    private fun call(a) {
        try {
            val intent = Intent(Intent.ACTION_CALL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)
        } catch (e: SecurityException) {
            e.printStackTrace()
        }
    }

    private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
Copy the code

Apply for permission using a third-party open source library (SoulPermission)

  1. Add a third-party library (SoulPermission) dependency to the module build.gradle file
implementation 'com.'ve: soulpermission: 1.3.0'
Copy the code
  1. Called in the activity
class MainActivity : AppCompatActivity() {

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

    /** * apply for permission before calling */
    private fun applyPermissionAndCall(a) {
        SoulPermission.getInstance()
            .checkAndRequestPermission(
                Manifest.permission.CALL_PHONE,
                object : CheckRequestPermissionListener {
                    override fun onPermissionOk(permission: Permission) {
                        call()
                    }

                    override fun onPermissionDenied(permission: Permission) {
                        toast("No permission, can't call 😢.")}}}/** * call system call function, dial 10086 */
    private fun call(a) {
        try {
            val intent = Intent(Intent.ACTION_CALL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)
        } catch (e: SecurityException) {
            e.printStackTrace()
        }
    }

    private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
Copy the code

Complete source code

Gitee.com/cxyzy1/Perm…

Example for applying for multiple rights

private fun applyPermissionAndPhoto(a) {
    SoulPermission.getInstance()
        .checkAndRequestPermissions(
            Permissions.build(
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ),
            object : CheckRequestPermissionsListener {
                override fun onAllPermissionOk(allPermissions: Array<out Permission>? {
                    photo()
                }

                override fun onPermissionDenied(refusedPermissions: Array<out Permission>?{}})}Copy the code

Android development tutorial series summary

Development language learning

Kotlin language basics

UI control learning series

UI control _TextView UI control _EditText UI control _Button UI control _ImageView UI control _RadioButton UI control _CheckBox UI control _ProgressBar

Follow the headlines to get the latest articles: