Kotlin

Requesting permissions

Because our app uses audio and video from the user's device, we’ll need to add some code to request audio and video permissions.

  1. Add permissions to AndroidManifest.xml

Inside your AndroidManifest.xml, add the following inside the <manifest> tag:

ℹ️ Note: The WAKE_LOCK permission is used to keep the device from sleeping during a video call. This ensures uninterrupted media streaming.

  1. Request runtime permissions in your MainActivity.kt

We’ll now update your MainActivity to request permissions at runtime using. At the top of your MainActivity.kt, define the permissions your app needs and a request code constant:

private val REQUIRED_PERMISSIONS = arrayOf(
    Manifest.permission.CAMERA,
    Manifest.permission.RECORD_AUDIO
)
  1. Define requestPermissionLauncher in in your MainActivity.kt. It will handle the permission response for us.
private val requestPermissionLauncher = registerForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
    val allGranted = permissions.values.all { it }
    if (allGranted) {
        // All permissions granted
        initializeSession()
    } else {
        // Permissions denied
        Toast.makeText(
            this,
            "Camera and microphone permissions are required to make video calls.",
            Toast.LENGTH_LONG
        ).show()
    }
}
  1. Check for permissions in onCreate()

In your onCreate() method, check whether all required permissions have been granted. If they have, proceed to initialize the video session. Otherwise, request the permissions.

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

    requestPermissions()
}

private fun requestPermissions() {
    if (hasPermissions()) {
        // Permissions are granted — proceed
        initializeSession()
    } else {
        // Request permissions using the modern approach
        requestPermissionLauncher.launch(REQUIRED_PERMISSIONS)
    }
}
  1. Implement the helper method hasPermissions()

Add this helper method to check if all required permissions are already granted:

private fun hasPermissions(): Boolean {
    return REQUIRED_PERMISSIONS.all {
        ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
    }
}
  1. Complete MainActivity.kt by creating a session placeholder

Define the initializeSession() method as a placeholder for your video call setup. Below is the complete MainActivity.kt with everything so far:

class MainActivity : ComponentActivity() {

    private val REQUIRED_PERMISSIONS = arrayOf(
        Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO
    )

    private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { permissions ->
        val allGranted = permissions.values.all { it }
        if (allGranted) {
            // All permissions granted
            initializeSession()
        } else {
            // Permissions denied
            Toast.makeText(
                this,
                "Camera and microphone permissions are required to make video calls.",
                Toast.LENGTH_LONG
            ).show()
        }
    }

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

        requestPermissions()
    }

    private fun requestPermissions() {
        if (hasPermissions()) {
            // Permissions are granted — proceed
            initializeSession()
        } else {
            // Request permissions using the modern approach
            requestPermissionLauncher.launch(REQUIRED_PERMISSIONS)
        }
    }

    // Helper function to check permissions
    private fun hasPermissions(): Boolean {
        return REQUIRED_PERMISSIONS.all {
            ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
        }
    }

    private fun initializeSession() {
        // TODO: Connect to Vonage session or set up video UI
    }
}
  1. Next Steps

Once permissions are granted, the initializeSession() function will be triggered. In the next steps of the tutorial, you'll implement the logic to connect to a Vonage Video API session.