阻止移动网站打开我的应用程序安卓深度链接 - Google Chrome

8

我已经在我的应用程序中支持了深度链接

<activity android:name=".DeepLinkActivity" android:noHistory="true"></activity>
    <activity-alias
        android:name="com.example.Launcher"
        android:targetActivity=".DeepLinkActivity">
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" />
            <data android:scheme="http" />
            <data android:scheme="@string/SCHEMA" />
            <data android:host="@string/WEB_DOMAIN" />
            <data android:host="@string/WWW_WEB_DOMAIN" />
            <data android:path="/" />
            <data android:path="/x" android:pathPattern="/x/.*" />
        </intent-filter>
    </activity-alias>

每当用户点击www.example.com时,安卓应用程序会要求打开应用程序或网页,这是可以接受的,但我不希望当我的用户在移动网站上时被要求打开应用程序。我已经阅读了许多stackoverflow的文章,但是每个人都说这是不可能的,但仍然有许多网站正在处理这种情况。
根据文章,行为取决于用户手势,如果用户点击任何链接,则Chrome会显示选择器,而如果用户在浏览器中键入URL,则不会显示选择器。

2
移动站点可能使用不同的URL? - Divers
你能展示一下那个网站和应用吗? - Divers
@Divers,目前我们只支持自定义模式而不是http和https,所以它没有从任何地方重定向,仅在应用程序内部。但在支持http和https之后,这个问题就出现了。 - Tasneem
2
你说“仍有许多网站处理这种情况”,我想要求你展示至少一个我可以检查的网站。 - Divers
请查看nl.pepper.com、myntra.com、hotUKdeals。 - Tasneem
显示剩余5条评论
1个回答

9

经过大量研究,我已经解决了这个问题。您可以使用任何一种方法。

在移动网站中处理:如果您希望在用户访问您的移动网站时始终将其呈现为Chrome,则可以通过将所有URL重定向到以下内容来实现:

在应用程序中处理:

  • 创建单个透明活动以处理所有深度链接

  • 使用pathpattern = '.*'处理所有链接

  • 对于您不想在应用程序中处理的URL,将用户重定向回Chrome。

AndroidManifest.xml

 <activity
            android:name=".DeepLinkActivity"
            android:noHistory="true"
            android:theme="@style/Theme.Transparent">
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" />
                <data android:scheme="http" />
                <data android:scheme="@string/SCHEMA" />
                <data android:host="@string/WEB_DOMAIN" />
                <data android:host="@string/WWW_WEB_DOMAIN" />
                <data android:pathPattern="/.*" />
            </intent-filter>
        </activity>

DeepLinkActivity

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    try {
        if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
            Uri uri = getIntent().getData();
            if (uri == null) throw new IllegalArgumentException("Uri can not be null");
            Intent intent = null;
            if (getString(R.string.SCHEMA).equals(uri.getScheme()) || uri.toString().matches(Links.REGEX)) {
                intent = linkIntent(uri);
            }
            if (intent == null) SystemUtil.launchUrlInDefaultBrowser(uri, this); else startActivity(intent);

        }
    } catch (IllegalArgumentException e) {
        Toast.makeText(this, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(this, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
    } finally {
        finish();
    }
}

 /**
 * This will open the provided url in browser except the current app.
 * @param url Uri
 * @param context  Activity Context
 */
public static void launchUrlInDefaultBrowser(Uri url, Context context) {
    try {
        ResolveInfo packageInfo = null;
        final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        browserIntent.setData(url);
        // Try to find anything that we can launch the URL with. Pick up the first one that can.
        final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
        if (!resolveInfoList.isEmpty()) {
            for (ResolveInfo list : resolveInfoList) {
                if (!BuildConfig.APP_PACKAGE_NAME.equals(list.activityInfo.packageName)) {
                    packageInfo = list;
                    break;
                }
            }
        }
        if (packageInfo != null) {
            browserIntent.setClassName(packageInfo.activityInfo.packageName, packageInfo.activityInfo.name);
            context.startActivity(browserIntent);
        } else {
            Toast.makeText(context, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        Toast.makeText(context, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
    }
}

1
这些解决方案对我来说看起来像是一个巨大的黑客。如果谷歌决定将默认浏览器切换到其他浏览器(就像他们曾经做过的那样),会怎么样?如果用户有其他(自定义)移动浏览器,会怎么样?如果设备制造商安装了其他默认浏览器,会怎么样? - SergGr
是的,这只是一个hack,但它运行良好。意图问题仅出现在Chrome中。 - Tasneem
你找到了合适的解决方案吗? - codepeaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接