代码之家  ›  专栏  ›  技术社区  ›  computingfreak itsnikolay

Android应用程序与启动同一活动的多个域链接

  •  2
  • computingfreak itsnikolay  · 技术社区  · 9 年前

    AndroidManifest.xml 文件

    <activity
        android:name=".activity.LaunchActivity"
        android:autoVerify="true"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
        android:label="@string/app_name"
        android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-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:host="subdomain1.domain.ext"
                android:path="/path1/subpath1/.*"
                android:scheme="https" />
        </intent-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:host="subdomain1.domain.ext"
                android:pathPattern="/path2/subpath2/..*"
                android:scheme="https" />
        </intent-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:host="subdomain2.subdomain1.domain.ext"
                android:pathPattern="^..*"
                android:scheme="https" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activity.LoginActivity"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
        android:label="@string/app_name" />
    <activity
        android:name=".activity.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
        android:label="@string/app_name" />
    

    我想在两个域中使用Android应用程序链接,

    1. 子域2.subdomain1。领域提取

    subdomain1.domain.ext/.well-known/assetlinks.json
    
    subdomain2.subdomain1.domain.ext/.well-known/assetlinks.json
    

    但在使用Android Studio安装应用程序时, 一、 到apache服务器的 .

    请注意,我使用的是release build变体,它使用的密钥库文件与在assetlinks中生成SHA256指纹时使用的密钥库文件相同。json。

    当我使用链接进行测试时,例如

    https://subdomain1.domain.ext/path1/subpath1/
    
    https://subdomain2.subdomain1.domain.ext/path2
    

    只有较低的一个启动应用程序,前者只是在浏览器中打开。

    问题1 在具有相同活动的不同主机上打开两个路径/链接 ? 在我的情况下,我如何使第一个链接也打开应用程序?

    . 我想 限制在应用程序中打开某些链接

    android:pathPattern="^(?!foo|bar)..*$"
    

    排除以foo和bar开头但允许其他路径的链接路径。

    example.com in web browser
    example.com/test in web browser
    example.com/test/temp in web browser
    example.com/{anything else} in the app
    

    我阅读了其他stackoverflow帖子:

    Android Deep linking omit certain url

    问题3 . 它是 强制性的

    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    

    期待您的帮助。Android团队不允许排除路径似乎太愚蠢了,他们应该从苹果的服务器端文件中学习,该文件使用了一个简单的not子句。

    public class LaunchActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splashscreen);
            Intent intent = getIntent();
            if (intent != null) {
                Uri target = intent.getData();
                if (target != null && target.isHierarchical()) {
                    String goal = intent.getDataString();
                    List<String> params = target.getPathSegments();
                    Log.d(TAG, "app uri : " + goal);
                    Log.d(TAG, "app link : " + target.toString());
                    Log.d(TAG, "path segments : " + params);
                    if (target.toString().startsWith(MON_DOM)) {
                        if (searchString(AppLinksUtil.TARGET_URLS, target.toString())) {
                            Log.d(TAG, "Open TARGET Link in APP");
                        } else {
                            Log.d(TAG, "Open Excluded Link in Browser");
                            openLinkInChrome(LaunchActivity.this, goal);
                            finish();
                        }
                    } else if (target.toString().startsWith(ENQ_DOM)) {
                        Log.d(TAG, "Exclude List : " + AppLinksUtil.ENQ_EXCLUDE_URLS);
                        if (searchString(AppLinksUtil.EXCLUDE_URLS, target.toString())) {
                            Log.d(TAG, "Open Excluded Link in Browser");
                            openLinkInChrome(LaunchActivity.this, goal);
                            finish();
                        } else {
                            Log.d(TAG, "Open Link in APP");
                        }
                    }
                }
            } else {
                Log.d(TAG, "no intent");
            }
            appFlow();
        }
        ...
        public void openLinkInChrome(final Activity activity, String link) {
            Log.d(TAG, "Opening " + link + " in web browser");
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setPackage("com.android.chrome");
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException ex) {
                intent.setPackage(null);
                activity.startActivity(intent);
            }
        }
        public static boolean searchString(String[] arr, String targetValue) {
            for (String s : arr) {
                if (targetValue.startsWith(s))
                    return true;
            }
            return false;
        }
    }
    
    2 回复  |  直到 9 年前
        1
  •  7
  •   computingfreak itsnikolay    8 年前

    在尝试一些代码更改时,我意识到

    对于1 ,同一个域的子域没有通配符选项,支持多个域,用正确的路径模式逐个列出它们,修复了我面临的问题。

    AndroidManifest.xml

    <activity android:name="MainActivity">
    <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="http" />
        <data android:host="subdomain1.example1.com" />
        <data android:host="subdomain2.example1.com" />
        <data android:host="subdomain3.example2.com" />
        <data android:path="/onlythis" />
        <data android:pathPrefix="/starter" />
        <data android:pathPattern="/prefix.*" />
        <data android:pathPattern="/.*suffix" />
    </intent-filter>
    </activity>
    

    对于2 ,排除路径还不可能,谷歌必须向苹果学习。

    ,任何活动都可以链接,而不仅仅是主/启动器活动。

    因此,只要在action=VIEW中定义了default和browsable,不同的域/子域就可以启动不同的活动

        2
  •  2
  •   David Wasser    9 年前

    pathPattern 不支持完整的正则表达式功能。它只支持“*”和“.*”。就是这样。

    路径模式 在里面 https://developer.android.com/guide/topics/manifest/data-element.html

    路径模式 Intent 对象,但它可以包含 以下通配符:

    • 后跟星号(“.*)的句点匹配0到多个字符的任何序列。

    另请参见 https://developer.android.com/reference/android/os/PatternMatcher.html#PATTERN_SIMPLE_GLOB


    你可以有很多 <intent-filter> <activity> . 由于无法使用正则表达式来描述所需的不同URL,因此只需创建一个单独的URL < 每个URL的说明。

    推荐文章