javascriptandroidkotlinwebviewshare-intent

Call Android share intent from JavaScript in webView


I am trying to use the code of this article to launch the share intent of my Android app (kotlin) from a JavaScript in a webView. Here is the code I use to start:

class MainActivity : AppCompatActivity() {  object AndroidJSInterface {
    @JavascriptInterface
    fun onClicked() {
      Log.d("HelpButton", "Help button clicked")
    }
  }  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)    val webViewClient = object : WebViewClient() {
      override fun onPageFinished(view: WebView, url: String) {
        loadJs(view)
      }
    }    val webView = WebView(this)
    webView.webViewClient = webViewClient
    webView.settings.javaScriptEnabled = true
    webView.addJavascriptInterface(AndroidJSInterface, "Android")    setContentView(webView)
    webView.loadUrl("https://vuetifyjs.com/en/getting-started/quick-start")
  }  private fun loadJs(webView: WebView) {
    webView.loadUrl(
      """javascript:(function f() {
        var btns = document.getElementsByTagName('button');
        for (var i = 0, n = btns.length; i < n; i++) {
          if (btns[i].getAttribute('aria-label') === 'Support') {
            btns[i].setAttribute('onclick', 'Android.onClicked()');
          }
        }
      })()"""
    )
  }
}

I modified the function in the AndroidJSInterface like this:

    @JavascriptInterface
    fun shareUrl(url: String? = "") {
        Log.d("JavaScript", "shareUrl: " + url)
        if( !url.isNullOrEmpty() )
        {
            val intent= Intent()
            intent.action=Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT, url)
            intent.type="text/plain"
            startActivity(Intent.createChooser(intent, "Share To:"))
        }
    }

Problem I have, is that startActivity is not recognized here (I get a "Unresolved reference: startActivity"). How can I access startActivity from this part of my code?

Thanks for your help,

Christophe


Solution

  • I found a solution, although I do not understand deeply why this works:

    I replaced:

    @JavascriptInterface
    fun shareUrl(url: String? = "") {
        Log.d("JavaScript", "shareUrl: " + url)
        if( !url.isNullOrEmpty() )
        {
            val intent= Intent()
            intent.action=Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT, url)
            intent.type="text/plain"
            startActivity(Intent.createChooser(intent, "Share To:"))
        }
    }
    

    By:

    private inner class JavascriptInterface
    {
        @android.webkit.JavascriptInterface
        fun shareUrl(url: String? = "") {
            Log.d("JavaScript", "shareUrl: " + url)
            // TODO: debug startActivity to launch Android sharing screen
            if( !url.isNullOrEmpty() )
            {
                val intent= Intent()
                intent.action=Intent.ACTION_SEND
                intent.putExtra(Intent.EXTRA_TEXT, url)
                intent.type="text/plain"
                this@WebActivity.startActivity(Intent.createChooser(intent, "Share To:"))
            }
        }
    }