简体   繁体   中英

How to Intent Whatsapp Opened In Webview?

I want to used Whatsapp as customer service. My apps is a webview , when the customer going to contact page, and see a number whatsapp and Chat Us button to chat with customer service.

When customer click the button, their didn't direct open whatsapp and showing ERR_UNKOWN_SCHEME_URL

I'm using android studio and webview to my apps using kotlin .

This is my activity :

class MainActivity : AppCompatActivity() {
    internal var webView: WebView? = null
    private var mCM: String? = null
    private var mUM: ValueCallback<Uri>? = null
    private var mUMA: ValueCallback<Array<Uri?>>? = null


    @SuppressLint("SetJavaScriptEnabled", "WrongViewCast")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView = findViewById(R.id.ifView) as WebView
        assert(webView != null)
        val webSettings = webView!!.settings
        webSettings.javaScriptEnabled = true
        webSettings.allowFileAccess = true

        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.mixedContentMode = 0
            webView!!.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        }
        else {
            webView!!.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        }
        webView!!.webViewClient = Callback()
        webView!!.loadUrl("http://example.com") //add your test web/page address here
        webView!!.setWebChromeClient(object : WebChromeClient() {

    SuppressWarnings("deprecation")
    @Override
    fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean
    {
        if (url != null && url.startsWith("whatsapp://")) {
            val sendIntent = Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Moon TV");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.whatsapp");
            startActivity(sendIntent)
            return true
            /* ContextCompat.startActivity(sendIntent);return true;*/

        } else {
            return false;
        }
    }

This is My Callback() code :

inner class Callback : WebViewClient() {
        override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
            Toast.makeText(applicationContext, "No Internet Access!", Toast.LENGTH_SHORT).show()
            view.loadUrl("file:///android_asset/error.html")            
        }
}

This is php code to Open Direct Chat Whatsapps Customer Service

    <a href="https://api.whatsapp.com/send?phone=628118906771&amp;text=Halo%20Example," class="btn btn-success btn-md">Visit WhatsApp</a>

I want Intent WhatsApp In Webview. Thank You.

From your MyActivity , remove this :

SuppressWarnings("deprecation")
    @Override
    fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean
    {
        if (url != null && url.startsWith("whatsapp://")) {
            val sendIntent = Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Moon TV");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.whatsapp");
            startActivity(sendIntent)
            return true
            /* ContextCompat.startActivity(sendIntent);return true;*/

        } else {
            return false;
        }

And, you add this code in your Callback() function or inner class Callback :

override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {

            var overrideUrlLoading = false

            if (url != null && url.startsWith("whatsapp://")) {

                view.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))

                overrideUrlLoading = true

            } else {

                view.loadUrl(url)
            }

            return overrideUrlLoading
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM