学习手札: 关于flattenToString。

在看代码的时候偶然看到了这个方法,望文生义,还是不知多云。
flatten: 击败,摧毁;使平坦

常见的用法是:
getIntent().getComponent().flattenToString();

那这到底是个什么意思呢

 /**
 * Return a String that unambiguously describes both the package and
 * class names contained in the ComponentName.  You can later recover
 * the ComponentName from this string through
 * {@link #unflattenFromString(String)}.
 *
 * @return Returns a new String holding the package and class names.  This
 * is represented as the package name, concatenated with a '/' and then the
 * class name.
 *
 * @see #unflattenFromString(String)
 */
public String flattenToString() {
    return mPackage + "/" + mClass;
}

简单来说,就是返回“包名/类名”这样一个字符串。其实更为常用的可能是flattenToShortString。

 /**
 * The same as {@link #flattenToString()}, but abbreviates the class
 * name if it is a suffix of the package.  The result can still be used
 * with {@link #unflattenFromString(String)}.
 *
 * @return Returns a new String holding the package and class names.  This
 * is represented as the package name, concatenated with a '/' and then the
 * class name.
 *
 * @see #unflattenFromString(String)
 */
public String flattenToShortString() {
    StringBuilder sb = new StringBuilder(mPackage.length() + mClass.length());
    appendShortString(sb, mPackage, mClass);
    return sb.toString();
}

/** @hide */
public void appendShortString(StringBuilder sb) {
    appendShortString(sb, mPackage, mClass);
}

/** @hide */
public static void appendShortString(StringBuilder sb, String packageName, String className) {
    sb.append(packageName).append('/');
    appendShortClassName(sb, packageName, className);
}

其实就是返回“包名/类名(不包含包名前缀)”:

    private static void appendShortClassName(StringBuilder sb, String packageName,
        String className) {
    if (className.startsWith(packageName)) {
        int PN = packageName.length();
        int CN = className.length();
        if (CN > PN && className.charAt(PN) == '.') {
            sb.append(className, PN, CN);
            return;
        }
    }
    sb.append(className);
}

对应的 unflattenFromString

 /**
 * Recover a ComponentName from a String that was previously created with
 * {@link #flattenToString()}.  It splits the string at the first '/',
 * taking the part before as the package name and the part after as the
 * class name.  As a special convenience (to use, for example, when
 * parsing component names on the command line), if the '/' is immediately
 * followed by a '.' then the final class name will be the concatenation
 * of the package name with the string following the '/'.  Thus
 * "com.foo/.Blah" becomes package="com.foo" class="com.foo.Blah".
 *
 * @param str The String that was returned by flattenToString().
 * @return Returns a new ComponentName containing the package and class
 * names that were encoded in <var>str</var>
 *
 * @see #flattenToString()
 */
public static ComponentName unflattenFromString(String str) {
    int sep = str.indexOf('/');
    if (sep < 0 || (sep+1) >= str.length()) {
        return null;
    }
    String pkg = str.substring(0, sep);
    String cls = str.substring(sep+1);
    if (cls.length() > 0 && cls.charAt(0) == '.') {
        cls = pkg + cls;
    }
    return new ComponentName(pkg, cls);
}

这些代码来自一个重要的类叫ComponentName,特定应用组件的标识,标识四大组件android.app.Activity,android.app.Service,android.content.BroadcastReceiver,android.content.ContentProvider
这个类里主要封装两方面信息,这两个信息是标识一个组件所必须的,可以说,有这两个信息就能找到这个组件,一是组件所在包的包名,二是组件的类名。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值