How to run href=tel

I am create webView and all fine, the problem is are not execute in phone href=tel:123456789, have the code

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_cito_plus.*
// import android.support.v4.content.ContextCompat.startActivity
import android.content.Intent
import android.net.Uri
import android.support.v4.content.ContextCompat.startActivity

class CitoPlus : AppCompatActivity() {

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val context = this
    setContentView(R.layout.activity_main)
    webview.webViewClient = MyWebViewClient()
    val webSettings = webview.getSettings()
    webSettings.javaScriptEnabled = true
    webview.loadUrl(URL)

}

@Suppress("OverridingDeprecateMember")
class MyWebViewClient : WebViewClient() {

    var context: Context? = null

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        if (request?.url.toString().startsWith("tel:")) {
            /*val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel:123")
            startActivity(intent)*/
            makeCall("123")
            return true
        }
        view?.loadUrl(request?.url.toString())
        return true
    }

    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        if (url!!.startsWith("tel:")) {
            makeCall("123")
            return false
        }
        view?.loadUrl(url)
        return true
    }

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

override fun onBackPressed() {
    if (webview.canGoBack()) {
        webview.goBack()
    } else {
        super.onBackPressed()
    }
}

}

but have error: Error:(64, 32) Type mismatch: inferred type is Intent but Context! was expected

some advide, who execute href=tel: in ACTION_DIAL ??

THANKS!!!

Your error states, that you are passing a variable of type Intent to a function which expects a parameter of type Context. Somewhere in you’re passing a variable of the wrong type.

I have personally not worked with “href=tel” but I am sure if you could tell us where in your code exactly the error is we can help you :wink:

Thank Wasabi

The problem es exact in

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

in

startActivity(intent)

here, i don’t how to send you the context,

class MyWebViewClient : WebViewClient() → fun makeCall(number:String): Boolean)

all the tuto say that this is how the href=tel dialing is done

thanks

So after a short google search (literally first result :wink:) I found this: android - error to startActivity(intent), whats going wrong? - Stack Overflow.
The answer there should give you all the information you need. It’s not exactly your situation, but close enough that.

Next time you might want to consider googling for the problem you have. I know this is not as easy as it sounds as you have to know what the problem is (and that takes experience). So let me explain my thought process.
There are 3 things I know about your problem. Your error states that you have a type mismatch between Intent and Context.
Second: you are running on android (I can tell from the imports)
And third you are calling a method called startActivity on an instance of WebViewClient.

As I personally have not used those yet I googled: “android webviewclient startactivity”. The first result is the link I posted above.
If you have any more problems with this feel free to ask, but I guarantee you, that searching is normally faster than asking questions in forums, because than you have to wait for someone to help you. Obviously this does not mean you should not ask if you don’t find any helpful answers :slight_smile:

PS: Another thing I like to point out is, the more code you post, the less likely will I be to answer you. Reading code takes time and just posting an entire file kind of says: “Hey I have a problem but I can’t be bothered to deal with it. Can you solve this for me?”. I don’t want to imply thats what you did, it just feels that way to me reading, because I can’t tell if you put any effort into finding the solution yourself.

if i googled but did my search very badly, i didn’t have the idea to search for startActivity, but beacuse of the failure that Android Studio was throwing me.

Sorry about that, i’ll be more cautions in my searches.

thank Wasabi.

i finally solved it this way.

    fun makeCall(view: WebView?, number: String): Boolean {
        return try {
            val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:123"))
            view!!.context.startActivity(intent)
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

:grin: sorry

I totally understand that. We all have been there. There is 100% no reason to apologize. What I tried to convey was that you could have asked the same question without posting the entire file. You could have just shown us the function with the error and explaining what you wanted it to do. And learning to search for the right terms is like an art in itself. In my opinion this is one of the harder parts of programming, knowing what to search for and how. I mean sure. After years you see that error message and know exactly what it means and how to fix it, but the only way to learn this is by trying and failing. This is why I told you the search string I used :slight_smile: Never feel bad about asking a question!!

1 Like

Btw, maybe Anko is a good library for you?
Anko provides a strong base for Android, but is unfortunately not maintained very well at the moment.
But the base provides a good platform which you can use to make for example the intents a lot easier.