【问题标题】:Block mobile website to open my app android deeplink - Google Chrome阻止移动网站打开我的应用 android deeplink - Google Chrome
【发布时间】:2017-02-17 15:12:01
【问题描述】:

我的应用支持深度链接

<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 时,Android 应用程序都会要求以应用程序或 Web 的形式打开,这很好,但我不希望当我的用户在移动网站上时,他们应该被要求在应用程序中打开。我浏览了许多 stackoverflow 帖子,但每个人都说这不可能,但仍有许多网站正在处理这种情况。

根据Article,行为取决于用户手势,如果用户点击任何链接,则 Chrome 会显示一个选择器,而如果用户在浏览器上输入 url,则不会。

【问题讨论】:

  • 可能是手机网站使用不同的url?
  • 没有移动网站和应用具有相同的网址。
  • 你能展示那个网站和应用吗?
  • www.desidime.com ,https 架构尚未生效,因此不适合您。
  • 你说still many websites are handling this scenario.,我要求至少给我看一个我可以检查的。

标签: android android-intent google-chrome-app deep-linking deeplink


【解决方案1】:

经过大量研究,我已经解决了。您可以使用任何一种方式。

在移动网站中处理:因此,如果您想在用户访问您的 msite 时始终将其呈现给 Chrome,您可以通过将所有 url 重定向到

来实现

在应用中处理

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

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

  • 将用户重定向回 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();
    }
}

【讨论】:

  • 这些解决方案对我来说就像一个巨大的黑客攻击。如果谷歌决定将他们的默认浏览器切换到其他浏览器(就像他们曾经做过的那样)怎么办?如果用户有其他(自定义)移动浏览器怎么办?如果设备制造商安装了其他东西作为默认浏览器怎么办?
  • 是的,这是一个 hack,但它工作正常。意图问题仅与 Chrome 有关。
  • 你找到合适的解决方案了吗?
猜你喜欢
  • 2020-11-27
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多