【问题标题】:Pass objects from main Activity to IntentService将对象从主 Activity 传递到 IntentService
【发布时间】:2013-05-29 15:24:47
【问题描述】:

我有一个主 Activity,它启动一个 IntentService 类 SchedulerWebSocketService.class

Intent webSockIntent = new Intent(getApplicationContext(), SchedulerWebSocketService.class);
startService(webSockIntent);

该服务运行一个 Web 套接字客户端。根据收到的消息,应用程序将解释消息,启动后台线程以开始新任务,例如拍照。主Activity包含各种参数,需要传递给IntentService,再传递给新的可运行线程。

我知道我可以使用 putExtra 来传输一些对象,例如字符串。如何传递对当前 Activity 或其他自定义对象的引用,这些对象在主 Activity 和由 IntentService 启动的新线程之间共享?

当我调用 new Intent(...) 时,有没有办法以某种方式修改意图服务的构造函数并将参数传递给这个新构造函数?

【问题讨论】:

    标签: android-intent android-service


    【解决方案1】:

    推荐的传递大对象的方法是bind to the service:一旦你这样做了,你的Activity就可以访问实际的服务,你可以调用服务上的方法并传递对象。

    【讨论】:

    • 谢谢你,Femi,这似乎是解决这个问题的正确方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多