Android deeplink打开本应用App

本文介绍如何在Android清单文件中配置启动活动及接收自定义URL意图,实现从浏览器跳转到App特定页面的方法。
清单文件中配置
    <activity
        android:name=".WelcomeActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!--被其他应用获取aaa ://bbb/ccc -->
        <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:host="bbb"
                android:path="/ccc"
                android:scheme="aaa" />
        </intent-filter>

    </activity>
  1. android:exported=“true”
    外部应用能够访问的到

  2. 被其他应用获取

<data
    android:host="bbb"
    android:path="/ccc"
    android:scheme="aaa" />

协议跳转

aaa ://bbb/ccc

3.在html中添加跳转index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <title></title>
    <meta name="Generator" content="Cocoa HTML Writer">
    <meta name="CocoaVersion" content="1561.4">
    <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 17.0px; font: 12.0px 'Songti SC'; color: #000000; -webkit-text-stroke: #000000; min-height: 17.0px}
    span.s1 {font-kerning: none}
  </style>
</head>
<body>
<a href="aaa ://bbb/ccc">打开main</a>
</html>

在浏览器中打开此文件,点击按钮自动跳转到App欢迎页

Android 应用中通过 Deep Link 打开特定的 Fragment 是一种常见的导航需求,尤其适用于从外部链接直接跳转应用内的某个具体页面。实现这一功能的关键在于使用 Navigation 组件与 Deep Link 的结合。 ### 使用 Navigation 组件配置 Deep Link 1. 在 `navigation.xml` 文件中为目标 Fragment 添加 Deep Link 配置: ```xml <fragment android:id="@+id/targetFragment" android:name="com.example.app.TargetFragment" android:label="TargetFragment" > <deepLink android:id="@+id/deepLink" app:uri="https://example.com/target" /> </fragment> ``` 上述代码定义了一个 URI 模式 `https://example.com/target`,当用户点击符合此模式的链接时,系统会启动该 Fragment [^2]。 2. 确保 `NavHostFragment` 已正确配置,并且关联了对应的 `navGraph` 属性指向你的导航图文件: ```xml <fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/navigation_graph" /> ``` 3. 如果需要处理额外参数,可以在 URI 中包含占位符: ```xml <deepLink android:id="@+id/deepLinkWithArgs" app:uri="https://example.com/target/{itemId}" /> ``` 这样可以传递动态数据给目标 Fragment,例如商品 ID 或用户 ID。 ### 处理来自 Deep Link 的 Intent - 在主 Activity 中重写 `onCreate()` 方法来触发 Deep Link 导航: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 NavHostFragment 和 NavController NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment); NavController navController = navHostFragment.getNavController(); // 处理传入的 Intent if (Intent.ACTION_VIEW.equals(getIntent().getAction())) { Uri data = getIntent().getData(); if (data != null && navController.getCurrentDestination() != null) { navController.navigate(data); // 自动匹配 deep link 并导航至相应 fragment } } } ``` ### 测试 Deep Link - 可以使用 ADB 命令测试 Deep Link 是否正常工作: ```bash adb shell am start -W -d https://example.com/target -a android.intent.action.VIEW com.example.app ``` 此命令模拟了用户点击网页链接的行为,并尝试启动相应的应用组件。 ### 注意事项 - **Manifest 设置**:确保在 `AndroidManifest.xml` 中声明了适当的 intent-filter 以支持 Deep Link: ```xml <activity android:name=".MainActivity"> <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://example.com" /> </intent-filter> </activity> ``` 这将允许系统识别并处理来自浏览器或其他应用的链接请求 [^2]。 - **App Links 验证**:为了提高安全性,建议启用 Android App Links 并完成网站所有权验证,这样可以避免其他应用劫持你的 Deep Link。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值