You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android开发:如何让分享的视频链接仅在自研App内播放?

让分享的视频链接直接在你的App中打开的解决方案

嗨,这个问题的核心是要让你的App成为对应视频链接的合法处理候选,这样用户点击分享的链接时,系统才会优先(或可选)用你的App打开。我来一步步给你拆解实现步骤:

1. 配置AndroidManifest.xml,让你的App能识别视频链接

首先要给负责播放视频的Activity添加intent-filter,告诉系统你的App可以处理这类视频链接。打开你的AndroidManifest.xml,找到播放视频的Activity(比如YourVideoPlayerActivity),添加以下配置:

<activity
    android:name=".YourVideoPlayerActivity"
    android:exported="true">
    <!-- 处理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="https"
            android:host="your-video-domain.com"
            android:pathPrefix="/video-content/" />
    </intent-filter>

    <!-- 可选:如果用自定义Scheme(比如yourapp://video),可以加这个filter -->
    <!--
    <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="yourapp"
            android:host="video" />
    </intent-filter>
    -->
</activity>

配置说明:

  • android.intent.action.VIEW:声明这个Activity可以处理“查看内容”的请求
  • android.intent.category.DEFAULT:让Activity能够被隐式Intent启动(系统跳转时会用到)
  • android.intent.category.BROWSABLE:允许从浏览器或其他可浏览应用中启动你的App
  • data节点:精准匹配你的视频链接格式,这样系统只会在点击符合该格式的链接时,把你的App列为候选

2. 在Activity中处理传入的链接

当用户点击链接打开你的App时,需要在播放页面获取链接并触发播放逻辑。在你的视频播放Activity中添加以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);
    
    // 处理启动时的Intent
    handleIncomingIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // 处理后续的Intent(比如App已在后台时被唤起)
    handleIncomingIntent(intent);
}

private void handleIncomingIntent(Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        Uri videoUri = intent.getData();
        if (videoUri != null) {
            // 调用你的视频播放方法,传入获取到的链接
            startVideoPlayback(videoUri.toString());
        }
    }
}

// 替换成你自己的播放逻辑
private void startVideoPlayback(String videoUrl) {
    // 比如初始化播放器、加载视频等操作
}

3. 可选:配置App Links实现无跳转直接打开(进阶)

如果想让用户点击链接时直接打开你的App,无需选择应用,可以配置Android App Links:

  1. intent-filter中添加android:autoVerify="true"
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data
        android:scheme="https"
        android:host="your-video-domain.com"
        android:pathPrefix="/video-content/" />
</intent-filter>
  1. 在你的域名的.well-known目录下放置assetlinks.json文件,内容示例:
[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.your.app.package.name",
    "sha256_cert_fingerprints": ["你的App签名的SHA256指纹"]
  }
}]

这个文件用于让系统验证你是该域名的所有者,验证通过后,用户点击链接就会直接打开你的App。

关于你当前的分享代码

你现在的分享代码不需要修改,只要完成上述配置,用户点击你分享的文本链接时,系统就会弹出应用选择框(或直接打开你的App,如果配置了App Links),用户选择你的App后就能直接在里面播放视频了。

内容的提问来源于stack exchange,提问作者Harish Rawal

火山引擎 最新活动