Android深层链接和应用链接记录

118 阅读1分钟

1.在需要跳转的Activity下添加intent-filter

    <activity
        android:name="com.example.android.GizmosActivity"
        android:label="@string/title_gizmos" >
        <intent-filter android:label="@string/filter_view_http_gizmos">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
            <data android:scheme="http"
                  android:host="www.example.com"
                  android:pathPrefix="/gizmos" />
            <!-- note that the leading "/" is required for pathPrefix-->
        </intent-filter>
        <intent-filter android:label="@string/filter_view_example_gizmos">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "example://gizmos” -->
            <data android:scheme="example"
                  android:host="gizmos" />
        </intent-filter>
    </activity>
    

scheme和host可以交叉组合,支持 https://www.example.com 和 app://open.my.app。支持 app://www.example.com 和 https://open.my.app

    <intent-filter>
      ...
      <data android:scheme="https" android:host="www.example.com" />
      <data android:scheme="app" android:host="open.my.app" />
    </intent-filter>
    
  1. 在Activity onCreat()里获取Intent,解析
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)

        val action: String? = intent?.action
        val data: Uri? = intent?.data
    }    

3.上传json文件到自己服务器www.xxx.com/.well-known… 格式:

  [{    "relation": ["delegate_permission/common.handle_all_urls"],
    "target": {
      "namespace": "android_app",
      "package_name": "com.mycompany.app1",
      "sha256_cert_fingerprints":
      ["14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5"]
    }
  }]

4.使用adb测试链接

adb shell am start -a android.intent.action.VIEW \
      -c android.intent.category.BROWSABLE \
      -d "http://domain.name:optional_port"
  

这里仅简单记录链接接入过程,具体路由跳转封装下次再见。