【问题标题】:Open Android app from URL using intent-filter not working for 4.1 version使用意图过滤器从 URL 打开 Android 应用程序不适用于 4.1 版本
【发布时间】:2015-11-25 07:06:57
【问题描述】:

我正在尝试使用 URL 打开一个 android 应用程序,该应用程序以前在所有其他设备上都可以正常工作,但它不适用于 samsung galaxy tab 10"。

这是我的代码:

<intent-filter>
    <data
    android:host="com.magzine.nu"
    android:scheme="magzine" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

并使用它 “杂志://com.magzine.nu/?magid=532&issueid=7474” url 打开应用程序,但是当单击 URL 时会出现一个空白屏幕并消失,而不是打开应用程序,任何建议将不胜感激。提前致谢。

【问题讨论】:

    标签: android url android-intent intentfilter


    【解决方案1】:

    实际上,自 Google Chrome version 25 以来,Google 确实弃用了该功能

    其他方式你可以在你的 url 中使用这样的 Android Intents:

    <a href="intent://read/#Intent;scheme=magzine;package=com.magzine.nu;end"> Will open magzine app</a>
    

    然后像这样设置清单

      <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="magzine" android:host="read" android:path="/"/>
      </intent-filter>
    

    更完整的参考你可以看看Zxing manifest(这是谷歌推荐的)

    然后解析部分你可以看看Intent Source Code

    尤其是这部分:

    /**
     * Convert this Intent into a String holding a URI representation of it.
     * The returned URI string has been properly URI encoded, so it can be
     * used with {@link Uri#parse Uri.parse(String)}.  The URI contains the
     * Intent's data as the base URI, with an additional fragment describing
     * the action, categories, type, flags, package, component, and extras.
     *
     * <p>You can convert the returned string back to an Intent with
     * {@link #getIntent}.
     *
     * @param flags Additional operating flags.  Either 0 or
     * {@link #URI_INTENT_SCHEME}.
     *
     * @return Returns a URI encoding URI string describing the entire contents
     * of the Intent.
     */
    public String toUri(int flags) {
        StringBuilder uri = new StringBuilder(128);
        String scheme = null;
        if (mData != null) {
            String data = mData.toString();
            if ((flags&URI_INTENT_SCHEME) != 0) {
                final int N = data.length();
                for (int i=0; i<N; i++) {
                    char c = data.charAt(i);
                    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
                            || c == '.' || c == '-') {
                        continue;
                    }
                    if (c == ':' && i > 0) {
                        // Valid scheme.
                        scheme = data.substring(0, i);
                        uri.append("intent:");
                        data = data.substring(i+1);
                        break;
                    }
    
                    // No scheme.
                    break;
                }
            }
            uri.append(data);
    
        } else if ((flags&URI_INTENT_SCHEME) != 0) {
            uri.append("intent:");
        }
    
        uri.append("#Intent;");
    
        toUriInner(uri, scheme, flags);
        if (mSelector != null) {
            uri.append("SEL;");
            // Note that for now we are not going to try to handle the
            // data part; not clear how to represent this as a URI, and
            // not much utility in it.
            mSelector.toUriInner(uri, null, flags);
        }
    
        uri.append("end");
    
        return uri.toString();
    }
    

    【讨论】:

    • 如果这是 chrome 更新的问题,那么这应该适用于设备浏览器,对吧?
    • @arslanhaktic 我认为它也不会影响设备浏览器,但老实说,使用这样的方案并不是打开应用程序的最佳方式。因为您不拥有该方案,任何人都可以使用它,而且那里也存在安全风险。
    • 您能否参考一些适当的文档/教程来解释使用它的最佳方式。我也想发送参数。
    【解决方案2】:

    在我的例子中,有一段代码每次都从后台打开应用程序,这导致应用程序一打开就关闭,并给人一种应用程序没有打开的印象。删除该代码后,它工作正常。所以上面的代码非常适合我的情况。 @NikoYuwono 提到的方法也更值得推荐。

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2013-11-20
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多