ComponentNamepublic final class ComponentName extends Object implements Comparable, android.os.Parcelable, CloneableIdentifier for a specific application component
({@link android.app.Activity}, {@link android.app.Service},
{@link android.content.BroadcastReceiver}, or
{@link android.content.ContentProvider}) that is available. Two
pieces of information, encapsulated here, are required to identify
a component: the package (a String) it exists in, and the class (a String)
name inside of that package. |
Fields Summary |
---|
private final String | mPackage | private final String | mClass | public static final Parcelable.Creator | CREATOR |
Constructors Summary |
---|
public ComponentName(String pkg, String cls)Create a new component identifier.
if (pkg == null) throw new NullPointerException("package name is null");
if (cls == null) throw new NullPointerException("class name is null");
mPackage = pkg;
mClass = cls;
| public ComponentName(Context pkg, String cls)Create a new component identifier from a Context and class name.
if (cls == null) throw new NullPointerException("class name is null");
mPackage = pkg.getPackageName();
mClass = cls;
| public ComponentName(android.os.Parcel in)Instantiate a new ComponentName from the data in a Parcel that was
previously written with {@link #writeToParcel(Parcel, int)}. Note that you
must not use this with data written by
{@link #writeToParcel(ComponentName, Parcel)} since it is not possible
to handle a null ComponentObject here.
mPackage = in.readString();
if (mPackage == null) throw new NullPointerException(
"package name is null");
mClass = in.readString();
if (mClass == null) throw new NullPointerException(
"class name is null");
| private ComponentName(String pkg, android.os.Parcel in)
mPackage = pkg;
mClass = in.readString();
| public ComponentName(Context pkg, Class cls)Create a new component identifier from a Context and Class object.
mPackage = pkg.getPackageName();
mClass = cls.getName();
|
Methods Summary |
---|
private static void | appendShortClassName(java.lang.StringBuilder sb, java.lang.String packageName, java.lang.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);
| public void | appendShortString(java.lang.StringBuilder sb)
appendShortString(sb, mPackage, mClass);
| public static void | appendShortString(java.lang.StringBuilder sb, java.lang.String packageName, java.lang.String className)
sb.append(packageName).append('/");
appendShortClassName(sb, packageName, className);
| public android.content.ComponentName | clone()
return new ComponentName(mPackage, mClass);
| public int | compareTo(android.content.ComponentName that)
int v;
v = this.mPackage.compareTo(that.mPackage);
if (v != 0) {
return v;
}
return this.mClass.compareTo(that.mClass);
| public int | describeContents()
return 0;
| public boolean | equals(java.lang.Object obj)
try {
if (obj != null) {
ComponentName other = (ComponentName)obj;
// Note: no null checks, because mPackage and mClass can
// never be null.
return mPackage.equals(other.mPackage)
&& mClass.equals(other.mClass);
}
} catch (ClassCastException e) {
}
return false;
| public java.lang.String | 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)}.
StringBuilder sb = new StringBuilder(mPackage.length() + mClass.length());
appendShortString(sb, mPackage, mClass);
return sb.toString();
| public java.lang.String | 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 mPackage + "/" + mClass;
| public java.lang.String | getClassName()Return the class name of this component.
return mClass;
| public java.lang.String | getPackageName()Return the package name of this component.
return mPackage;
| public java.lang.String | getShortClassName()Return the class name, either fully qualified or in a shortened form
(with a leading '.') if it is a suffix of the package.
if (mClass.startsWith(mPackage)) {
int PN = mPackage.length();
int CN = mClass.length();
if (CN > PN && mClass.charAt(PN) == '.") {
return mClass.substring(PN, CN);
}
}
return mClass;
| public int | hashCode()
return mPackage.hashCode() + mClass.hashCode();
| private static void | printShortClassName(java.io.PrintWriter pw, java.lang.String packageName, java.lang.String className)
if (className.startsWith(packageName)) {
int PN = packageName.length();
int CN = className.length();
if (CN > PN && className.charAt(PN) == '.") {
pw.write(className, PN, CN-PN);
return;
}
}
pw.print(className);
| public static void | printShortString(java.io.PrintWriter pw, java.lang.String packageName, java.lang.String className)
pw.print(packageName);
pw.print('/");
printShortClassName(pw, packageName, className);
| public static android.content.ComponentName | readFromParcel(android.os.Parcel in)Read a ComponentName from a Parcel that was previously written
with {@link #writeToParcel(ComponentName, Parcel)}, returning either
a null or new object as appropriate.
String pkg = in.readString();
return pkg != null ? new ComponentName(pkg, in) : null;
| public java.lang.String | toShortString()Return string representation of this class without the class's name
as a prefix.
return "{" + mPackage + "/" + mClass + "}";
| public java.lang.String | toString()
return "ComponentInfo{" + mPackage + "/" + mClass + "}";
| public static android.content.ComponentName | unflattenFromString(java.lang.String str)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".
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);
| public void | writeToParcel(android.os.Parcel out, int flags)
out.writeString(mPackage);
out.writeString(mClass);
| public static void | writeToParcel(android.content.ComponentName c, android.os.Parcel out)Write a ComponentName to a Parcel, handling null pointers. Must be
read with {@link #readFromParcel(Parcel)}.
if (c != null) {
c.writeToParcel(out, 0);
} else {
out.writeString(null);
}
|
|