Most visited

Recently visited

Added in API level 1

ComponentName

public final class ComponentName
extends Object implements Parcelable, Cloneable, Comparable<ComponentName>

java.lang.Object
   ↳ android.content.ComponentName


标识符为特定的应用程序组件( ActivityServiceBroadcastReceiver ,或ContentProvider可用)。 需要在这里封装两条信息来标识组件:它存在的包(String)以及该包内的类(String)名称。

Summary

Inherited constants

From interface android.os.Parcelable

Fields

public static final Creator<ComponentName> CREATOR

Public constructors

ComponentName(String pkg, String cls)

创建一个新的组件标识符。

ComponentName(Context pkg, String cls)

从上下文和类名创建新的组件标识符。

ComponentName(Context pkg, Class<?> cls)

从Context和Class对象创建一个新的组件标识符。

ComponentName(Parcel in)

从之前用 writeToParcel(Parcel, int)写入的Parcel中的数据实例化新的ComponentName。

Public methods

ComponentName clone()

创建并返回此对象的副本。

int compareTo(ComponentName that)
static ComponentName createRelative(String pkg, String cls)

创建一个新的组件标识符,其中类名称可以被指定为绝对的或相对于包含的包。

static ComponentName createRelative(Context pkg, String cls)

创建一个新的组件标识符,其中类名称可以被指定为绝对的或相对于包含的包。

int describeContents()

描述此Parcelable实例的封送表示中包含的特殊对象的种类。

boolean equals(Object obj)

指示其他某个对象是否“等于”这一个。

String flattenToShortString()

flattenToString()相同,但如果它是包的后缀,则缩写为类名。

String flattenToString()

返回一个明确描述ComponentName中包含的包名和类名的字符串。

String getClassName()

返回此组件的类名称。

String getPackageName()

返回此组件的包名称。

String getShortClassName()

如果它是包的后缀,则返回完全限定的类名或缩写形式(带有“。”)。

int hashCode()

返回对象的哈希码值。

static ComponentName readFromParcel(Parcel in)

从先前使用 writeToParcel(ComponentName, Parcel)编写的Parcel中读取ComponentName,根据需要返回null或new对象。

String toShortString()

返回此类的字符串表示形式,但不将类的名称作为前缀。

String toString()

返回对象的字符串表示形式。

static ComponentName unflattenFromString(String str)

从先前使用 flattenToString()创建的字符串中恢复ComponentName。

static void writeToParcel(ComponentName c, Parcel out)

写一个ComponentName到一个Parcel,处理空指针。

void writeToParcel(Parcel out, int flags)

将此对象平铺到一个包裹中。

Inherited methods

From class java.lang.Object
From interface android.os.Parcelable
From interface java.lang.Comparable

Fields

CREATOR

Added in API level 1
Creator<ComponentName> CREATOR

Public constructors

ComponentName

Added in API level 1
ComponentName (String pkg, 
                String cls)

创建一个新的组件标识符。

Parameters
pkg String: The name of the package that the component exists in. Can not be null.
cls String: The name of the class inside of pkg that implements the component. Can not be null.

ComponentName

Added in API level 1
ComponentName (Context pkg, 
                String cls)

从上下文和类名创建新的组件标识符。

Parameters
pkg Context: A Context for the package implementing the component, from which the actual package name will be retrieved.
cls String: The name of the class inside of pkg that implements the component.

ComponentName

Added in API level 1
ComponentName (Context pkg, 
                Class<?> cls)

从Context和Class对象创建一个新的组件标识符。

Parameters
pkg Context: A Context for the package implementing the component, from which the actual package name will be retrieved.
cls Class: The Class object of the desired component, from which the actual class name will be retrieved.

ComponentName

Added in API level 1
ComponentName (Parcel in)

从之前用writeToParcel(Parcel, int)写入的Parcel中的数据实例化新的ComponentName。 请注意,由于无法在此处理null ComponentObject,因此您不得将此数据用于由writeToParcel(ComponentName, Parcel)写入的数据。

Parameters
in Parcel: The Parcel containing the previously written ComponentName, positioned at the location in the buffer where it was written.

Public methods

clone

Added in API level 1
ComponentName clone ()

创建并返回此对象的副本。 “复制”的确切含义可能取决于对象的类别。 一般意图是,对于任何对象x ,表达式:

 x.clone() != x
will be true, and that the expression:
 x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
 x.clone().equals(x)
will be true, this is not an absolute requirement.

按照惯例,返回的对象应该通过调用super.clone获得。 如果一个类和它的所有超类( Object除外)都遵守这个约定,那将是x.clone().getClass() == x.getClass()

按照惯例,这个方法返回的对象应该独立于这个对象(被克隆)。 要实现这种独立性,可能需要在返回super.clone之前修改返回的对象的一个或多个字段。 通常,这意味着复制包含被克隆对象的内部“深层结构”的任何可变对象,并将这些对象的引用替换为对这些副本的引用。 如果一个类仅包含原始字段或对不可变对象的引用,那么通常情况下不需要修改由super.clone返回的对象中的字段。

Object的方法clone执行特定的克隆操作。 首先,如果该对象的类没有实现接口Cloneable ,则引发CloneNotSupportedException 请注意,所有数组都被认为是实现接口Cloneable并且数组类型T[]clone方法的返回类型是T[] ,其中T是任何引用或基本类型。 否则,此方法创建该对象的类的新实例,并使用该对象的相应字段的内容来初始化其所有字段,就像通过赋值一样; 这些字段的内容本身并不克隆。 因此,此方法执行此对象的“浅拷贝”,而不是“深拷贝”操作。

Object本身并不实现接口 Cloneable ,所以在类为 Object的对象上调用 clone方法将导致在运行时抛出异常。

Returns
ComponentName a clone of this instance.

compareTo

Added in API level 4
int compareTo (ComponentName that)

Parameters
that ComponentName
Returns
int

createRelative

Added in API level 23
ComponentName createRelative (String pkg, 
                String cls)

创建一个新的组件标识符,其中类名称可以被指定为绝对的或相对于包含的包。

相对软件包名称以'.'字符开头。 对于包"com.example"和类名称".app.MyActivity"此方法将返回一个ComponentName与包"com.example"和类名称"com.example.app.MyActivity" 完全合格的课程名称也是允许的。

Parameters
pkg String: the name of the package the component exists in
cls String: the name of the class inside of pkg that implements the component
Returns
ComponentName the new ComponentName

createRelative

Added in API level 23
ComponentName createRelative (Context pkg, 
                String cls)

创建一个新的组件标识符,其中类名称可以被指定为绝对的或相对于包含的包。

相对软件包名称以'.'字符开头。 对于程序包"com.example"和类名称".app.MyActivity"此方法将返回ComponentName与程序包"com.example"和类名称"com.example.app.MyActivity" 完全合格的课程名称也是允许的。

Parameters
pkg Context: a Context for the package implementing the component
cls String: the name of the class inside of pkg that implements the component
Returns
ComponentName the new ComponentName

describeContents

Added in API level 1
int describeContents ()

描述此Parcelable实例的封送表示中包含的特殊对象的种类。 例如,如果对象将在writeToParcel(Parcel, int)的输出中包含writeToParcel(Parcel, int) ,则此方法的返回值必须包含CONTENTS_FILE_DESCRIPTOR位。

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.

equals

Added in API level 1
boolean equals (Object obj)

指示其他某个对象是否“等于”这一个。

equals方法在非空对象引用上实现等价关系:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

equals方法Object实现了对象上最有区别的可能等价关系; 也就是说,对于任何非空参考值xy ,此方法返回true当且仅当xy引用同一对象( x == y具有值true )。

请注意,无论何时覆盖此方法,通常都需要覆盖 hashCode方法,以便维护 hashCode方法的一般合同,该方法声明相等对象必须具有相同的散列代码。

Parameters
obj Object: the reference object with which to compare.
Returns
boolean true if this object is the same as the obj argument; false otherwise.

flattenToShortString

Added in API level 1
String flattenToShortString ()

flattenToString()相同,但如果它是包的后缀,则缩写为类名。 结果仍然可以与unflattenFromString(String)一起使用。

Returns
String 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.

也可以看看:

flattenToString

Added in API level 1
String flattenToString ()

返回一个明确描述ComponentName中包含的包名和类名的字符串。 您可以稍后通过unflattenFromString(String)从此字符串中恢复ComponentName。

Returns
String 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.

也可以看看:

getClassName

Added in API level 1
String getClassName ()

返回此组件的类名称。

Returns
String

getPackageName

Added in API level 1
String getPackageName ()

返回此组件的包名称。

Returns
String

getShortClassName

Added in API level 1
String getShortClassName ()

如果它是包的后缀,则返回完全限定的类名或缩写形式(带有“。”)。

Returns
String

hashCode

Added in API level 1
int hashCode ()

返回对象的哈希码值。 为了散列表的好处而支持此方法,例如由HashMap提供的HashMap

hashCode的总合同是:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

尽可能合理实用,由类Object定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术。)

Returns
int a hash code value for this object.

readFromParcel

Added in API level 1
ComponentName readFromParcel (Parcel in)

从先前用 writeToParcel(ComponentName, Parcel)写入的Parcel中读取ComponentName,根据需要返回null或new对象。

Parameters
in Parcel: The Parcel from which to read the ComponentName
Returns
ComponentName Returns a new ComponentName matching the previously written object, or null if a null had been written.

也可以看看:

toShortString

Added in API level 1
String toShortString ()

返回此类的字符串表示形式,但不将类的名称作为前缀。

Returns
String

toString

Added in API level 1
String toString ()

返回对象的字符串表示形式。 通常, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

toString类的方法Object返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”和对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

unflattenFromString

Added in API level 1
ComponentName unflattenFromString (String str)

从先前使用flattenToString()创建的字符串中恢复ComponentName。 它在第一个'/'处分割字符串,将之前的部分作为包名称,将后面的部分作为类名称。 作为一种特殊的便利(例如,在命令行上解析组件名称时使用),如果'/'后面紧跟'。' 那么最终的类名将是包名与字符串“/”后面的串联。 因此“com.foo/.Blah”变成package =“com.foo”class =“com.foo.Blah”。

Parameters
str String: The String that was returned by flattenToString().
Returns
ComponentName Returns a new ComponentName containing the package and class names that were encoded in str

也可以看看:

writeToParcel

Added in API level 1
void writeToParcel (ComponentName c, 
                Parcel out)

写一个ComponentName到一个Parcel,处理空指针。 必须与readFromParcel(Parcel)一起阅读。

Parameters
c ComponentName: The ComponentName to be written.
out Parcel: The Parcel in which the ComponentName will be placed.

也可以看看:

writeToParcel

Added in API level 1
void writeToParcel (Parcel out, 
                int flags)

将此对象平铺到一个包裹中。

Parameters
out Parcel: The Parcel in which the object should be written.
flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

Hooray!