You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中实现应用接收其他应用分享URL的方法

Android 应用接入外部URL分享/打开能力实现方案

1. 清单文件配置Intent过滤器

要让你的应用出现在系统分享、链接打开的可选列表里,首先要给接收跳转的Activity配置正确的intent-filter,注意Android 12+必须显式设置exported=true,否则外部应用无法唤起组件。

<activity
    android:name=".ReceiveShareActivity"
    android:exported="true">
    <!-- 匹配普通文本分享:覆盖YouTube等绝大多数应用的分享URL场景 -->
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>

    <!-- 可选配置:匹配http/https链接点击,让应用出现在「打开链接」的可选列表 -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" />
        <data android:scheme="https" />
        <!-- 如果只需要匹配特定域名,补充host配置即可,比如只接YouTube链接就加下面这行 -->
        <!-- <data android:host="youtube.com" android:pathPrefix="/" /> -->
    </intent-filter>
</activity>

注意:<category android:name="android.intent.category.DEFAULT" />是隐式Intent匹配的必加项,漏写的话应用永远不会出现在可选列表里

2. 在Activity中解析接收到的URL

用户选择你的应用后,系统会唤起对应Activity,你需要在生命周期回调里解析Intent拿到内容,注意如果Activity配置了singleTop、singleTask等启动模式,还要重写onNewIntent处理复用场景的Intent。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_receive_share)
    parseIntent(intent)
}

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    parseIntent(intent)
}

private fun parseIntent(intent: Intent?) {
    if (intent == null) return
    when (intent.action) {
        // 处理其他应用分享过来的文本
        Intent.ACTION_SEND -> {
            if (intent.type == "text/plain") {
                val shareContent = intent.getStringExtra(Intent.EXTRA_TEXT) ?: return
                // 部分应用分享的文本除了URL还会带标题、说明文案,需要用正则提取有效URL
                val targetUrl = pickUrlFromContent(shareContent)
                targetUrl?.let {
                    // 这里写拿到URL后的业务逻辑,比如路由跳转、存储、解析等
                    println("接收到分享URL:$it")
                }
            }
        }
        // 处理用户直接点击http/https链接唤起的场景
        Intent.ACTION_VIEW -> {
            val targetUrl = intent.data?.toString() ?: return
            println("接收到打开链接请求:$targetUrl")
        }
    }
}

// 从混合文本中提取合法URL的简易实现
private fun pickUrlFromContent(content: String): String? {
    val urlPattern = Regex("(https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])")
    return urlPattern.find(content)?.value
}

常见问题排查

  • 配置完不显示在分享列表:先检查exported是否设为true,是否加了DEFAULT category,再到系统应用信息页-默认打开设置里,检查是否禁用了应用的链接唤起权限,部分国产ROM默认会关闭这个权限。
  • 拿到的分享内容不是纯URL:大部分应用分享时会在EXTRA_TEXT里拼接URL+说明文案,必须做正则提取,不能直接把整个字段当URL用。
  • 不想出现在所有分享/链接打开场景:在intent-filter的data标签里配置特定host、mimeType,缩小匹配范围即可,比如只接收图片分享就把mimeType改成image/*,只接特定站点链接就加对应host配置。

内容的提问来源于stack exchange,提问作者Rohit Kushvaha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 21:06:22