1

I want to call an activity from a HTML page in my Android application. What would I put into

<a href="TestActivity.java">Test  Activity</a>
rheema
  • 41
  • 9
  • 3
    Possible duplicate of [About starting Android app from an URL](http://stackoverflow.com/questions/7416864/about-starting-android-app-from-an-url) – Manuel Allenspach Apr 06 '17 at 06:04

2 Answers2

1

Use an <intent-filter> with a <data> element. Put this inside your <activity> in your AndroidManifest.xml:

<intent-filter>
    <data android:scheme="my.special.scheme" />
    <action android:name="android.intent.action.VIEW" />
</intent-filter>

Then, in your web app you can put links like:

<a href="my.special.scheme://other/parameters/here">

When a user clicks on the link, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme:// type URIs). The only downside is that if the user doesn't have the app installed, they'll get an error.

  • thank you for the answer so "my.special.scheme" is the name of scheme but I don't understand "other" " parameters" and "here" – rheema Apr 05 '17 at 21:33
  • You can just ignore them. These are optional parameters that you can provide to your activity if necessary (e.g. the username of the person clicking on the link, or other data that you want your Android app to receive) – Sunny Varkas Apr 05 '17 at 21:50
1

I think the best way is overridden URL via webviewclient -> shouldOverrideUrlLoading

override fun shouldOverrideUrlLoading(view: WebView ? , request : WebResourceRequest ? ): Boolean {
  val url: String = request ? .url.toString()
  if ((Uri.parse(url).getHost().contains("myactivity"))) {
      val myIntent = Intent(context, MainActivity::class.java)
      context.startActivity(myIntent)
      return true
  }
  return false
}

HTML: <a href="http://myactivity">LINK</a>

Sebin Benjamin
  • 1,758
  • 2
  • 21
  • 45
wspeedy
  • 63
  • 1
  • 7