Skip to content

Instantly share code, notes, and snippets.

@maxost
Created September 5, 2017 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxost/bd2121c8306a4deae5a9dce1098f889e to your computer and use it in GitHub Desktop.
Save maxost/bd2121c8306a4deae5a9dce1098f889e to your computer and use it in GitHub Desktop.
Kotlin: make phone call in Android
fun Context.makePhoneCall(number: String) : Boolean {
try {
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$number"))
startActivity(intent)
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
@Nataanthoni
Copy link

Nataanthoni commented Jun 29, 2020

private fun checkPermission() {
if (ContextCompat.checkSelfPermission(requireActivity(),
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {

        // Permission is not granted
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(),
                        Manifest.permission.CALL_PHONE)) {
            Toast.makeText(activity, "Please grant My App permission to call.", Toast.LENGTH_SHORT).show()
            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(requireActivity(),
                    arrayOf(Manifest.permission.CALL_PHONE),
                    42)
        }
    } else {
        // Permission has already been granted
        Toast.makeText(activity, "Calling My App now ...", Toast.LENGTH_SHORT).show()
        callPhone()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment