在看代码的时候偶然看到了这个方法,望文生义,还是不知多云。
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
这个类里主要封装两方面信息,这两个信息是标识一个组件所必须的,可以说,有这两个信息就能找到这个组件,一是组件所在包的包名,二是组件的类名。