首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >意图锚语法描述

意图锚语法描述
EN

Stack Overflow用户
提问于 2014-04-22 22:40:59
回答 2查看 4.1K关注 0票数 7

我试图使用意图锚启动我的应用程序,如描述的here。我能让它用这个语法启动我的应用程序,

代码语言:javascript
复制
<a href="intent://#Intent;scheme=http;package=com.example.myapp;end">Launch my app</a>

但我不知道许多不同的元素意味着什么。

基于意图的URI的基本语法如下:

意图: 主机/URI-路径//可选主机#意图;package=string;action=string;category=string;component=string;scheme=string;end;

  1. 每个片段意味着什么(所以我知道如何最好地利用它们)
  2. 如何/在哪里包含任何额外的数据(即我自己的参数)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-23 19:12:35

下面是来自toUri()类的方法Intent

代码语言:javascript
复制
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;");
    if (scheme != null) {
        uri.append("scheme=").append(scheme).append(';');
    }
    if (mAction != null) {
        uri.append("action=").append(Uri.encode(mAction)).append(';');
    }
    if (mCategories != null) {
        for (String category : mCategories) {
            uri.append("category=").append(Uri.encode(category)).append(';');
        }
    }
    if (mType != null) {
        uri.append("type=").append(Uri.encode(mType, "/")).append(';');
    }
    if (mFlags != 0) {
        uri.append("launchFlags=0x").append(Integer.toHexString(mFlags)).append(';');
    }
    if (mPackage != null) {
        uri.append("package=").append(Uri.encode(mPackage)).append(';');
    }
    if (mComponent != null) {
        uri.append("component=").append(Uri.encode(
                mComponent.flattenToShortString(), "/")).append(';');
    }
    if (mSourceBounds != null) {
        uri.append("sourceBounds=")
                .append(Uri.encode(mSourceBounds.flattenToString()))
                .append(';');
    }
    if (mExtras != null) {
        for (String key : mExtras.keySet()) {
            final Object value = mExtras.get(key);
            char entryType =
                    value instanceof String    ? 'S' :
                    value instanceof Boolean   ? 'B' :
                    value instanceof Byte      ? 'b' :
                    value instanceof Character ? 'c' :
                    value instanceof Double    ? 'd' :
                    value instanceof Float     ? 'f' :
                    value instanceof Integer   ? 'i' :
                    value instanceof Long      ? 'l' :
                    value instanceof Short     ? 's' :
                    '\0';
            if (entryType != '\0') {
                uri.append(entryType);
                uri.append('.');
                uri.append(Uri.encode(key));
                uri.append('=');
                uri.append(Uri.encode(value.toString()));
                uri.append(';');
            }
        }
    }
    uri.append("end");
    return uri.toString();
}

如果您可以阅读Java代码,那么应该非常清楚这里发生了什么。在任何情况下,可以在URL中添加额外的内容,它们看起来如下所示:

代码语言:javascript
复制
<type>.<key>=<value>;

其中<type>是以下内容之一:

代码语言:javascript
复制
S = String
B = Boolean
b = Byte
c = Character
d = Double
f = Float
i = Integer
l = Long
s = Short

以下是几个例子:

启动应用程序:

代码语言:javascript
复制
<a href="intent://#Intent;scheme=http;package=com.example.myapp;end"> 

启动包含“bar123”值的额外字符串"foo“的应用程序:

代码语言:javascript
复制
<a href="intent://#Intent;scheme=http;package=com.example.myapp;S.foo=bar123;end"> 

启动包含值"bar123“的字符串"foo”和包含值“-567”的名为“数字”的整数加号的应用程序:

代码语言:javascript
复制
<a href="intent://#Intent;scheme=http;package=com.example.myapp;S.foo=bar123;i.number=-567;end"> 
票数 11
EN

Stack Overflow用户

发布于 2018-12-21 07:03:46

在android 28中,API已经发生了变化。下面是将toUri代码更改为:

代码语言:javascript
复制
public String toUri(@UriFlags int flags) {
    StringBuilder uri = new StringBuilder(128);
    if ((flags&URI_ANDROID_APP_SCHEME) != 0) {
        if (mPackage == null) {
            throw new IllegalArgumentException(
                    "Intent must include an explicit package name to build an android-app: "
                    + this);
        }
        uri.append("android-app://");
        uri.append(mPackage);
        String scheme = null;
        if (mData != null) {
            scheme = mData.getScheme();
            if (scheme != null) {
                uri.append('/');
                uri.append(scheme);
                String authority = mData.getEncodedAuthority();
                if (authority != null) {
                    uri.append('/');
                    uri.append(authority);
                    String path = mData.getEncodedPath();
                    if (path != null) {
                        uri.append(path);
                    }
                    String queryParams = mData.getEncodedQuery();
                    if (queryParams != null) {
                        uri.append('?');
                        uri.append(queryParams);
                    }
                    String fragment = mData.getEncodedFragment();
                    if (fragment != null) {
                        uri.append('#');
                        uri.append(fragment);
                    }
                }
            }
        }
        toUriFragment(uri, null, scheme == null ? Intent.ACTION_MAIN : Intent.ACTION_VIEW,
                mPackage, flags);
        return uri.toString();
    }
    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 >= '0' && c <= '9') || c == '.' || 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:");
    }

    toUriFragment(uri, scheme, Intent.ACTION_VIEW, null, flags);

    return uri.toString();
}

这表明所选择的答案将不再起作用。

检查this answer by David Wasser是否有工作解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23231589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档