【问题标题】:Best practice for making third-party class parcelable?使第三方类可打包的最佳实践?
【发布时间】:2018-10-26 01:59:57
【问题描述】:

我正在使用推送通知第三方解决方案(特别是来自 Urban Airship 推送通知库的 RichPushMessage 类)。我创建了一个要传递的包,其中包含一个 RichPushMessage 字段。我需要它来实现Parcelable。我不能继承RichPushMessage,因为它的构造函数是私有的。如何最好地让这个类可打包?

【问题讨论】:

    标签: android parcelable urbanairship.com


    【解决方案1】:

    即使您可以对其进行子类化,您也无法创建 RichPushMessage,因为它由 Urban Airship SDK 处理。

    我不确定是否可以按照您的要求做,但这里有一些替代方案:

    1) 将消息 ID 存储在包中:

    // Put it into the bundle
    Bundle bundle = new Bundle();
    bundle.putString(MESSAGE_ID_KEY, message.getMessageId());
    
    // Pull it out of the bundle
    String messageId = bundle.getString(MESSAGE_ID_KEY);
    RichPushMessage message = UAirship.shared().getInbox().getMessage(messageId);
    

    2) 创建一个包装 RichPushMessage 的类,您可以将其打包

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      相关资源
      最近更新 更多