开发者

Is it OK to use a static member in an IntentService to facilitate passing received data to an Activity?

开发者 https://www.devze.com 2023-02-22 11:51 出处:网络
I have class that extends \'IntentService\' - this class occasionally receives data from a server, and when it does I need to pass this data to the current running Activity.

I have class that extends 'IntentService' - this class occasionally receives data from a server, and when it does I need to pass this data to the current running Activity.

To achieve this, I've added a static instance of my Activity to the IntentService, along with a registerRecipient method that the Activity calls, passing in self. When the IntentService gets a message from the server, it just calls the appropriate notification method on its static instance of the Activity (in which I set an EditText view's text property via runOnUiThread).

Aside from the general problem of using static variables instead of instances, is this a sound way to do this? One of my colleagues suggested that this static instance is still subject to being garbage-collected, wh开发者_运维问答ich would hose the whole scenario.


For this scenario you do not need a static member variable of your Activity. What you need is a BroadcastReceiver, which corresponds with this question.


You could also fire off and Intent from the IntentService and handle the Intent in the receiving Activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消