I am developing an Android application and a website. What I am trying to do now is that I like to open the specific activity of Android application from the browser when the user click on a link.
This is my Android activity class
class SphereViewerActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sphere_viewer)
intent.getStringExtra("image_url")
}
}
As you can see in my code, I am getting the image_url parameter from the browser. Is it possible to open that activity passing the parameter from the JavaScript or browser?
I found the solution, it is to have the link like this
<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;S.browser_fallback_url=http%3A%2F%2Fzxing.org;end"> Take a QR code </a>
But how can I pass intent data as parameter?
I tried adding the app links. not working when I click Test App Links
You have 4 options to achieve what you want:
- Deep Links
- Android App Links
- Firebase Dynamic Links
- Firebase App Indexing
Refer to this post to see more descriptions about them.
In simplest approach (Deep Links), you can introduce your Activity
as a handler of specific pattern URL
s and pass the desired parameters as URL
query params.
AndroidManifest.xml
<activity android:name=".SphereViewerActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "myapp://zxing" -->
<data android:host="zxing" />
<data android:scheme="myapp" />
</intent-filter>
</activity>
SphereViewerActivity.kt
class SphereViewerActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sphere_viewer)
if (intent != null && intent.action == Intent.ACTION_VIEW) {
intent.data?.apply{
if (getQueryParameter("image_url") != null && getQueryParameter("image_url").isNotEmpty()) {
val imageUrl = data.getQueryParameter ("image_url") as String
// do what you want to do with imageUrl
}
}
}
}
}
Your html snippet:
<a href="myapp://zxing?image_url=some_image_url"> Take a QR code </a>