将对象从主Activity传递给IntentService

时间:2013-05-29 15:24:48

标签: android-intent android-service

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

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