Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我使用意图作为我的 Android 应用程序各个部分之间通信的消息传递机制。
我的问题是:我可以将 Intent 子类化以在其上构建自己的类,或者当我在广播接收器中获得它时它将成为标准 Intent 类?
我当然是在谈论进程内通信。但是交叉线程。
我自己从来没有这样做过,但我想这是可能的。查看LabeledIntent 文档和来源- 我认为这与您所描述的相似。在接收器中处理您的自定义Intent时,您需要将Intent参数onReceive()转换为您自己的子类。
LabeledIntent
Intent
onReceive()
如果您将任何字段添加到您的子类,您需要确保正确实现这些Parcelable方法 - 确保您 override writeToParcel(),有一个完全接受 onParcel参数的构造函数,并且有一个public static final Parcelable.Creator对象。
Parcelable
writeToParcel()
Parcel
public static final Parcelable.Creator