i want to be able to call a specific preset number using a button in my app Here is my code but when I run the app the button does do anything
val button3 = findViewById<Button>(R.id.button3)
button3.setOnClickListener {
val number = "*#0*#"
val intent = Intent(Intent.ACTION_DIAL)
intent.setData(Uri.parse("tel:" + "*#0*#"))
startActivity(intent)
}
I also did put the permission
\<uses-permission android:name="android.permission.CALL_PHONE"/\>
in the manifest.
all the forums online i could find did not work
i want to be able to call a specific preset number using a button in my app Here is my code but when I run the app the button does do anything
val button3 = findViewById<Button>(R.id.button3)
button3.setOnClickListener {
val number = "*#0*#"
val intent = Intent(Intent.ACTION_DIAL)
intent.setData(Uri.parse("tel:" + "*#0*#"))
startActivity(intent)
}
I also did put the permission
\<uses-permission android:name="android.permission.CALL_PHONE"/\>
in the manifest.
all the forums online i could find did not work
Try this
button3.setOnClickListener {
val phoneNumber = "*#0*#"
val intent = Intent(Intent.ACTION_DIAL).apply {
data = Uri.parse("tel:$phoneNumber")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
The Phone app's dialer is good at normalizing schemes, such as telephone numbers. So the scheme described isn't strictly required in the
Uri.parse()
method. However, if you haven't tried a scheme or are unsure whether it can be handled, use the
Uri.fromParts()
method instead
fore more details refer Initiate a phone call