0

I am trying to make an option in my app that calls 911, but when I click on the text in the lazycolumn that I should call it takes me to the phone system application and just there I can press to call 911. How can I get him to call that emergency number without taking me there?

     fun makeACall(context: Context, phoneNumber: String) {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) ==
            PackageManager.PERMISSION_GRANTED
        ) {
            val intent = Intent(Intent.ACTION_CALL)
            intent.data = Uri.parse("tel: $phoneNumber")
            startActivity(context, intent, bundleOf())
        } else {
            ActivityCompat.requestPermissions(
                this, arrayOf(Manifest.permission.CALL_PHONE), 777
            )
        }


    }


    @Preview
    @Composable
    fun Columna() {
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Gray)
        )
        {
            item {
                PhoneNumber(title = "911 (Emergencias)", phoneNumber = "911")


             }
        }
    }

    @Composable
    fun PhoneNumber(title: String, phoneNumber: String) {
    val context = LocalContext.current
    Text(
        text = title,
        fontSize = 32.sp,
        color = Color.White,
        modifier = Modifier
            .fillMaxWidth()
            .clickable { makeACall(context = context, phoneNumber = phoneNumber) },
        textAlign = TextAlign.Center
        )
}
LEMILEMI
  • 63
  • 1
  • 6
  • 3
    I don't think Android lets apps do this for what should be obvious reasons. I know iOS certainly does not allow this. – Dai Nov 01 '21 at 13:38
  • In France the emergency number is 112. I am not sure you are legally allowed to contact them "automatically". – Basile Starynkevitch Nov 01 '21 at 13:40
  • Does it work for other phone numbers? I have very similar code and it works fine. But I never tried emergency numbers for obvious reasons. Maybe it's prevented by Android, which I can imagine very well. I wouldn't even dare to test it with emergency numbers. Where I live it's illegal to call it without a real emergency – Ivo Nov 01 '21 at 13:44
  • Thanks everyone, I guess I'll leave the app like that and to call 911 go to the call keyboard – LEMILEMI Nov 01 '21 at 14:15

1 Answers1

2

Short answer: No, you cannot do this unless you are a system app.

To place a call without going through the dialer interface you need to have CALL_PRIVILEGED permission for your app. And this permission cannot be obtained by third-party applications as per the documentation.

viv3k
  • 621
  • 4
  • 10
  • 2
    Hijack the kernel. Break into the phone's mainframe - you can put your app in the system apps' category. You know Android is open source afterall. – Richard Onslow Roper Nov 01 '21 at 15:37