通过浏览器打开/启动通过意图参数的特定Android活动 [英] Opening/starting a specific Android activity passing an intent parameter from the browser

查看:146
本文介绍了通过浏览器打开/启动通过意图参数的特定Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序和一个网站.我现在想做的是,当用户单击链接时,我想从浏览器中打开Android应用程序的特定活动.

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.

这是我的Android活动课程

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")

    }
}

正如您在我的代码中看到的那样,我正在从浏览器获取image_url参数.是否可以通过JavaScript或浏览器传递参数来打开该活动?

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

推荐答案

您有4种选择来实现所需的目标:

You have 4 options to achieve what you want:

  1. 深层链接
  2. Android应用链接
  3. Firebase动态链接
  4. Firebase应用索引

请参阅这篇文章以查看有关它们的更多说明.

Refer to this post to see more descriptions about them.

在最简单的方法(深层链接)中,您可以将Activity引入为特定模式URL的处理程序,并将所需的参数作为URL查询参数进行传递.

In simplest approach (Deep Links), you can introduce your Activity as a handler of specific pattern URLs 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
                }
            }
        }
    }
}

您的html代码段:

<a href="myapp://zxing?image_url=some_image_url"> Take a QR code </a>

这篇关于通过浏览器打开/启动通过意图参数的特定Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆