0

Why i get an error on intent? I want to call a number when a clicking on a floating button.

ContextCompat.startActivity(intent), here a get the error (intent)

Type mismatch. Required:Context Found:Intent

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

    fab.setOnClickListener { view ->
        Snackbar.make(view, "Secretariaat wordt gebeld", 
Snackbar.LENGTH_LONG)
            .setAction("Action", null).show()
        makePhoneCall("0123456")
    }

    val toggle = ActionBarDrawerToggle(
        this, drawer_layout, toolbar, R.string.navigation_drawer_open, 
R.string.navigation_drawer_close
    )
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()

    nav_view.setNavigationItemSelectedListener(this)
   }





fun makePhoneCall(number: String) : Boolean {
    try {
        val intent = Intent(Intent.ACTION_CALL)
        intent.setData(Uri.parse("tel:$number"))
        ContextCompat.startActivity(intent)
        return true
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 2
    Why do you use `ContextCompat.startActivity(intent)`? Just use `this.startActivity(intent)`. I don't even think `this` is necessary. – HB. Jun 09 '19 at 08:41
  • Yes, you are right, I don't get the error anymore, but it isn't still calling the phone number!! – KingMong007 Jun 09 '19 at 09:21
  • That wasn't your question. Accept one of the answers and ask a new question. – HB. Jun 09 '19 at 17:39

3 Answers3

1

That's because ContextCompat.startActivity takes three arguments, Context, Intent and a Bundle as extra options (can be null)

ContextCompat.startActivity(this, intent, null)
Vladimír Bielený
  • 2,795
  • 2
  • 11
  • 16
0

If you need to invoke method startActivity(), you can do it without ContextCompat class. If you call this method inside Activity class. In that case your code will be like:

fun makePhoneCall(number: String) : Boolean {
    try {
        val intent = Intent(Intent.ACTION_CALL)
        intent.setData(Uri.parse("tel:$number"))
        startActivity(intent)
        return true
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
}
samaromku
  • 560
  • 2
  • 7
0
  1. No need to use static ContextCompat.startActivity(intent) use only startActivity(intent) as you are already in an Activity
  2. To use Intent.ACTION_CALL you need call permission in manifest.

<uses-permission android:name="android.permission.CALL_PHONE"/>

There is another solution which I prefer. Use Intent.ACTION_DIAL instead of Intent.ACTION_CALL which doesn't require permission.

Your code would be:

fun makePhoneCall(number: String) : Boolean {
    try {
        val intent = Intent(Intent.ACTION_DIAL)
        intent.setData(Uri.parse("tel:$number"))
        startActivity(intent)
        return true
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
}

More Info

Sayem
  • 4,891
  • 3
  • 28
  • 43
  • ok it works but now I get my keyboard and I want him to call immediately and not to have to confirm again by clicking on the phone-icon. – KingMong007 Jun 09 '19 at 11:45
  • I think that's another problem. You can google it according to your need or ask another question. You can accept my answer if it's helpful. Thanks. – Sayem Jun 09 '19 at 11:57
  • as far I know there is no such option. As this might be a security breach & user need to pay for it. If you really need it than you might need to implement the whole thing in your app. – Sayem Jun 09 '19 at 12:10