首页 > 解决方案 > 从android中的另一个应用程序共享时如何检索链接?

问题描述

您好,我正在尝试检索从另一个应用程序共享的 url,并对其进行敬酒以及在 WebViewTab 中打开它。但是应用程序的 id 显示在 toast 中。这是我的代码:

  val extras = intent.extras
        if (extras != null) {
            for (key in activity.intent.extras!!.keySet()) {
                CopyKey = key

                val value: String? = activity.intent.extras!!.getString(CopyKey)
                Toast.makeText(applicationContext, value, Toast.LENGTH_LONG).show()
                    TabInfo.addTab(value.toString())
                
            }
            val url = intent.extras!!.getString("query")
            if (url.toString().startsWith("http")) {
                TabInfo.addTab(url.toString())
                intent.removeExtra("query")
            }
        }

提前致谢

标签: androidkotlinandroid-intentbrowserandroid-browser

解决方案


这解决了我的问题:

val extras = intent.extras
        if (extras != null) {
            val externalUrl: Uri? = intent?.data //retrieves the shared text from another app.
            val url = intent.extras!!.getString("query")
            if (url.toString().startsWith("http")) {
                TabInfo.addTab(url.toString())
                intent.removeExtra("query")
            }
            else {
                TabInfo.addTab(externalUrl.toString())
            }
        }

推荐阅读