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
    

    相关文章

      网友评论

          本文标题:DeepLink

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