Xtophe
Xtophe

Reputation: 2277

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

Upvotes: 0

Views: 2084

Answers (2)

Xtophe
Xtophe

Reputation: 2277

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"
            [email protected](Intent.createChooser(intent, "Share To:"))
        }
    }
}

Upvotes: 0

rahat
rahat

Reputation: 2056

public abstract void startActivity(@RequiresPermission Intent intent);

This method is originally available in Context class and then in Activity and ContextWrapper as they are children of Context class.

AndroidJSInterface is a simple object, not a direct or indirect child of Context, hence startActivity is not recognized in that block since this in that method refers to the instance of AndroidJSInterface type which is not a child of Context. So to refer to the this or the instance of the MainActivity in shareUrl method.

You can do this in Kotlin

[email protected](Intent.createChooser(intent, "Share To:"))

or in Java

MainActivity.this.startActivity(Intent.createChooser(intent, "Share To:"));

Upvotes: 1

Related Questions