Android深度链接配置问题:点击可变参数URL无法唤起应用
Alright, let's break down why your current configuration isn't working and how to fix it. The key problem here is how Android handles the # (fragment) in your URL.
Why Your Current Setup Fails
Your target URL is https://myportal.net/#!/search/main/34. Notice the # symbol? Everything after it is considered a URI fragment—Android's <intent-filter> doesn't use fragments to match <data> attributes like pathPattern. Your existing pathPattern="/.*/.*/.*/.*" is trying to match parts of the URL's path, but in this case, the actual path (before the #) is empty. So the system never triggers your SplashActivity.
Solution 1: Adjust the Intent Filter & Parse the Fragment
Since you can't change the URL structure (with the #!), we'll modify the intent filter to match just the scheme and host, then manually parse the fragment in code to extract the variable number.
Step 1: Update the Intent Filter
Change your <intent-filter> to only match the https scheme and your host. Remove the pathPattern since it won't work with fragments:
<activity android:name="ui.Activities.SplashActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:theme="@style/SplashScreen"> <intent-filter android:label="@string/app_name"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Match only scheme and host; fragment will be handled in code --> <data android:host="${hostName}" android:scheme="https" /> </intent-filter> </activity>
Make sure ${hostName} is correctly resolved to myportal.net in your build config, or replace it directly with the host string for testing.
Step 2: Parse the Fragment in Your Activity
In SplashActivity, retrieve the fragment from the intent's URI and extract the variable number:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Handle deep link intent Intent incomingIntent = getIntent(); Uri deepLinkUri = incomingIntent.getData(); if (deepLinkUri != null) { String fragment = deepLinkUri.getFragment(); // The fragment will be "!/search/main/34" from your URL if (fragment != null && fragment.startsWith("!/")) { // Strip the leading "!/" to get the path segments String pathSegments = fragment.substring(2); String[] parts = pathSegments.split("/"); // Check if we have the expected segments if (parts.length >= 3) { String variableId = parts[2]; // This will be "34" (or any other number) // Now navigate to your target screen with this ID Intent targetIntent = new Intent(this, YourTargetActivity.class); targetIntent.putExtra("ITEM_ID", variableId); startActivity(targetIntent); finish(); // Optional: close SplashActivity after navigation } } } // Rest of your SplashActivity initialization code }
Step 3: Test the Deep Link
Use this adb command to test if the link opens your app correctly:
adb shell am start -W -a android.intent.action.VIEW -d "https://myportal.net/#!/search/main/34" com.your.app.package
Replace com.your.app.package with your app's actual package name.
Bonus: If You Can Modify the URL
If you have control over the URL structure, remove the #! to make it a standard path: https://myportal.net/search/main/34. Then you can use a pathPattern like "/search/main/.*" to directly match the variable:
<data android:host="${hostName}" android:pathPattern="/search/main/.*" android:scheme="https" />
This way, you can extract the ID directly from deepLinkUri.getLastPathSegment() without parsing the fragment.
内容的提问来源于stack exchange,提问作者Ahmad Nasser

