[英]how to retrieve a link when shared from another app in 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")
}
}
提前致謝
這解決了我的問題:
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())
}
}
檢查以下代碼片段的詳細信息
筆記:
val URL_FINDER_REGEX = "((http:\\/\\/|https:\\/\\/|ftp:\\/\\/|file:\\/\\/)?(www.)?(([a-zA-Z0-9-]){2,2083}\\.){1,4}([a-zA-Z]){2,6}(\\/(([a-zA-Z-_\\/\\.0-9#:?=&;,]){0,2083})?){0,2083}?[^ \\n]*)"
fun test(intent: Intent?) {
intent?.let {
it.extras?.let { extras ->
val urlQuery = extras.getString("query")
urlQuery?.let {
val links = getUrlsFromText(urlQuery)
println(links)
// TODO("Your Business logic")
}
}
}
}
fun getUrlsFromText(text: String): ArrayList<URI> {
val availableLinks = ArrayList<URI>()
val matcher: Matcher = Pattern.compile(URL_FINDER_REGEX, Pattern.CASE_INSENSITIVE).matcher(text)
while (matcher.find()) {
try {
val url = URI(matcher.group())
availableLinks.add(url)
} catch (e: Exception) {
println(e)
}
}
return availableLinks
}
這類似於一些舊查詢。
參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.