DeepLink

作者: TomyZhang | 来源:发表于2020-07-02 22:31 被阅读0次

一、Intent基础

//AndroidManifest.xml
<activity android:name=".PopupActivity">
    <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="tomorrow"
            android:scheme="app" />
    </intent-filter>
</activity>

//MainActivity
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
intent.setData(Uri.parse("app://tomorrow/path?param1=111&param2=222"));
startActivity(intent);

//PopupActivity
Intent intent = getIntent();
Log.d(TAG, "zwm, intent.toString:\n" + intent);
Log.d(TAG, "zwm, intent.getAction:\n" + intent.getAction());
Log.d(TAG, "zwm, intent.getData:\n" + intent.getData());
Log.d(TAG, "zwm, intent.getCategories:\n" + intent.getCategories());

String uri = intent.toUri(0);
Log.d(TAG, "zwm, intent.toUri:\n" + uri);

try {
    Log.d(TAG, "zwm, intent.parseUri:\n" + Intent.parseUri(uri, 0));
} catch (URISyntaxException e) {
    e.printStackTrace();
}

//日志打印
2020-07-01 10:39:30.617 29998-29998/com.tomorrow.target28 D/PopupActivity: zwm, intent.toString:
    Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=app://tomorrow/path?param1=111&param2=222 cmp=com.tomorrow.target28/.PopupActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } }
2020-07-01 10:39:30.617 29998-29998/com.tomorrow.target28 D/PopupActivity: zwm, intent.getAction:
    android.intent.action.VIEW
2020-07-01 10:39:30.617 29998-29998/com.tomorrow.target28 D/PopupActivity: zwm, intent.getData:
    app://tomorrow/path?param1=111&param2=222
2020-07-01 10:39:30.617 29998-29998/com.tomorrow.target28 D/PopupActivity: zwm, intent.getCategories:
    {android.intent.category.BROWSABLE}
2020-07-01 10:39:30.617 29998-29998/com.tomorrow.target28 D/PopupActivity: zwm, intent.toUri:
    app://tomorrow/path?param1=111&param2=222#Intent;category=android.intent.category.BROWSABLE;component=com.tomorrow.target28/.PopupActivity;end
2020-07-01 10:39:30.618 29998-29998/com.tomorrow.target28 D/PopupActivity: zwm, intent.parseUri:
    Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=app://tomorrow/path?param1=111&param2=222 cmp=com.tomorrow.target28/.PopupActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } }

二、DeepLink基础

实现方式1:(DeepLink为Uri格式)

//AndroidManifest.xml
<activity android:name=".DeepLinkActivity">
    <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="tomorrow"
            android:scheme="app" />
    </intent-filter>
</activity>
<activity android:name=".TargetActivity"/>

//MainActivity
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
intent.setData(Uri.parse("app://tomorrow/LaunchActivity?component=com.tomorrow.target28/.TargetActivity"));
startActivity(intent);

//DeepLinkActivity
Intent intent = getIntent();
Uri data = intent.getData();
Log.d(TAG, "zwm, intent.getData:\n" + data);
Log.d(TAG, "zwm, intent.getScheme:\n" + data.getScheme());
Log.d(TAG, "zwm, intent.getHost:\n" + data.getHost());
Log.d(TAG, "zwm, intent.getPath:\n" + data.getPath());
Log.d(TAG, "zwm, intent.getPathSegments:\n" + data.getPathSegments());
Log.d(TAG, "zwm, intent.getLastPathSegment:\n" + data.getLastPathSegment());
Log.d(TAG, "zwm, intent.getQuery:\n" + data.getQuery());
Log.d(TAG, "zwm, intent.getQueryParameter:\n" + data.getQueryParameter("component"));

Intent targetIntent = new Intent();
targetIntent.setComponent(ComponentName.unflattenFromString(data.getQueryParameter("component")));
targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(targetIntent);

//TargetActivity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("TargetActivity", "zwm, onCreate");
}

//日志打印
2020-07-01 13:48:58.492 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getData:
    app://tomorrow/LaunchActivity?component=com.tomorrow.target28/.TargetActivity
2020-07-01 13:48:58.492 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getScheme:
    app
2020-07-01 13:48:58.492 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getHost:
    tomorrow
2020-07-01 13:48:58.492 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getPath:
    /LaunchActivity
2020-07-01 13:48:58.493 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getPathSegments:
    [LaunchActivity]
2020-07-01 13:48:58.493 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getLastPathSegment:
    LaunchActivity
2020-07-01 13:48:58.493 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getQuery:
    component=com.tomorrow.target28/.TargetActivity
2020-07-01 13:48:58.493 20318-20318/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getQueryParameter:
    com.tomorrow.target28/.TargetActivity
2020-07-01 13:48:58.648 20318-20318/com.tomorrow.target28 D/TargetActivity: zwm, onCreate

实现方式2:(DeepLink为Intent格式)

//AndroidManifest.xml
<activity android:name=".DeepLinkActivity">
    <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="tomorrow"
            android:scheme="app" />
    </intent-filter>
</activity>
<activity android:name=".TargetActivity"/>

//MainActivity
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
intent.setData(Uri.parse("app://tomorrow#Intent;action=android.intent.action.VIEW;component=com.tomorrow.target28/.TargetActivity;S.extra_name=Android;S.extra_uri=https://www.baidu.com;end"));
startActivity(intent);

//DeepLinkActivity
Intent intent = getIntent();
Log.d(TAG, "zwm, intent: \n" + intent);
Uri data = intent.getData();
Log.d(TAG, "zwm, intent.getData:\n" + data);
Log.d(TAG, "zwm, intent.getScheme:\n" + data.getScheme());
Log.d(TAG, "zwm, intent.getHost:\n" + data.getHost());

String dataStr = intent.getDataString();
dataStr = dataStr.replace("app", "intent");
Log.d(TAG, "zwm, dataStr: \n" + dataStr);

Intent targetIntent = null;
try {
    targetIntent = Intent.parseUri(dataStr, 0);
    Log.d(TAG, "zwm, targetIntent: \n" + targetIntent);
} catch (URISyntaxException e) {
    e.printStackTrace();
}
Log.d(TAG, "zwm, targetIntent.getComponent: \n" + targetIntent.getComponent());
String extra_name = targetIntent.getExtras().getString("extra_name");
Log.d(TAG, "zwm, extra_name: \n" + extra_name);
String extra_uri = targetIntent.getExtras().getString("extra_uri");
Log.d(TAG, "zwm, extra_uri: \n" + extra_uri);

Intent startIntent = new Intent();
startIntent.setComponent(targetIntent.getComponent());
startIntent.putExtra("extra_name", extra_name);
startIntent.putExtra("extra_uri", extra_uri);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);

//TargetActivity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("TargetActivity", "zwm, onCreate");
}

//日志打印
2020-07-01 14:43:55.326 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent: 
    Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=app://tomorrow cmp=com.tomorrow.target28/.DeepLinkActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } }
2020-07-01 14:43:55.326 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getData:
    app://tomorrow#Intent;action=android.intent.action.VIEW;component=com.tomorrow.target28/.TargetActivity;S.extra_name=Android;S.extra_uri=https://www.baidu.com;end
2020-07-01 14:43:55.326 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getScheme:
    app
2020-07-01 14:43:55.326 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, intent.getHost:
    tomorrow
2020-07-01 14:43:55.327 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, dataStr: 
    intent://tomorrow#Intent;action=android.intent.action.VIEW;component=com.tomorrow.target28/.TargetActivity;S.extra_name=Android;S.extra_uri=https://www.baidu.com;end
2020-07-01 14:43:55.327 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, targetIntent: 
    Intent { act=android.intent.action.VIEW dat=//tomorrow cmp=com.tomorrow.target28/.TargetActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) }
2020-07-01 14:43:55.327 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, targetIntent.getComponent: 
    ComponentInfo{com.tomorrow.target28/com.tomorrow.target28.TargetActivity}
2020-07-01 14:43:55.327 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, extra_name: 
    Android
2020-07-01 14:43:55.327 32003-32003/com.tomorrow.target28 D/DeepLinkActivity: zwm, extra_uri: 
    https://www.baidu.com
2020-07-01 14:43:55.480 32003-32003/com.tomorrow.target28 D/TargetActivity: zwm, onCreate

相关文章

  • Web端向App端导量神技Deferred DeepLink的实

    1.什么是DeepLink 2.什么是Deferred DeepLink 3.Deferred DeepLink的...

  • Android DeepLink 技术

    DeepLink 是什么 DeepLink 从字面意思可以理解为「深度链接」,那么 DeepLink 在 Andr...

  • DeepLink

    DeepLink 深度链接 什么是DeepLink DeepLink,又称深度链接、调起链接,是一套链接服务,用户...

  • DeepLink

    一、Intent基础 二、DeepLink基础 实现方式1:(DeepLink为Uri格式) 实现方式2:(Dee...

  • Deeplink使用

    一:deeplink DeepLink: 深度链接技术,主要应用场景是通过Web页面直接调用Android原生ap...

  • Deeplink实践原理分析

    目录介绍 01.先看一个场景 02.什么是DeepLink 03.什么是Deferred DeepLink 04....

  • scheme协议

    先说下DeepLink(深度链接) DeepLink,又称深度链接、调起链接,是一套链接服务,用户点击链接可以跳转...

  • deepLink

    写在前面 我们在使用deepLink的流程是这样的:(1)在A应用内配置了B应用的URLScheme,那么我们就可...

  • deeplink

    deeplink 1.什么是deeplink? 简而言之,就是你在手机浏览器上面点击一个链接,可以跳转到另一个ap...

  • deepLink iOS 应用到自己APP 记录

    deepLink iOS 应用到自己APP 记录 1.了解deeplink 详细的介绍可以在网上查询,这里简单说一...

网友评论

      本文标题:DeepLink

      本文链接:https://www.haomeiwen.com/subject/pfidqktx.html