发送消息IntentService到活动发送消息、IntentService

2023-09-05 05:12:35 作者:阳光透过指尖丶耀眼,

我有一个活性和在同一应用程序的intentService。该服务必须保持运行活动结束,所以我不希望绑定后。我一直在谷歌上搜索了几个小时,找不到如何做到这一个很好的例子。我能够启动该服务,并通过临时演员,但目前该服务已经使用Messenger来将数据发送到活动中。

I have an activity and an intentService in the same application. The service must keep running after the activity ends so I do not want to bind. I have been googling for hours and can't find a single good example of how to do this. I'm able to start the service and pass extras to it but now the service has to use Messenger to send data back to the activity.

我看,这个过程主要涉及... 调用Message.obtain()来得到一个空的Message对象 填充与任何数据被需要的对象 调用send()的使者,提供消息作为参数

I read that this process basically involves... Calling Message.obtain() to get an empty Message object Populating that object with whatever data is needed Calling send() on the Messenger, supplying the message as a parameter

但我无法找到如何做到这一点任何code的例子。

But I can't find any code examples on how to do this.

几个职位是指在SDK示例APIDemos,我有一个messengerService的例子,但我找不到任何那里。 谢谢,加里

several posts refer to a messengerService example in the SDK sample APIDemos, which I have, but I can't find anything there. thanks, Gary

推荐答案

有关的记录,我会回答我的问题,因为它可能是有用的人... (我使用的是常规的服务,而不是一个IntentService,因为它需要保持活跃)

For the record I'll answer my own question as it might be useful to others... (I'm using a regular Service, not an IntentService as it needs to stay active)

有关从服务接收消息的活动,它必须实例化一个处理程序,所以......

For the activity to receive messages from the service, it has to instantiate a Handler as so...

   private Handler handler = new Handler() 
{
    public void handleMessage(Message message) 
    {
        Object path = message.obj;

        if (message.arg1 == 5 && path != null)
        {
            String myString = (String) message.obj;
            Gson gson = new Gson();
            MapPlot mapleg = gson.fromJson(myString, MapPlot.class);
            String astr = "debug";
            astr = astr + " ";
        }
    };
};

以上code由我调试的东西。该服务将消息发送到活动为使...

The above code consists of my debug stuff. The service sends the message to the activity as so...

                MapPlot mapleg = new MapPlot();
            mapleg.fromPoint = LastGeoPoint;
            mapleg.toPoint = nextGeoPoint;              
            Gson gson = new Gson();
            String jsonString = gson.toJson(mapleg); //convert the mapleg class to a json string
            debugString = jsonString;

            //send the string to the activity
            Messenger messenger = (Messenger) extras.get("MESSENGER");
            Message msg = Message.obtain();  //this gets an empty message object

            msg.arg1 = 5;
            msg.obj = jsonString;
            try
            {
                messenger.send(msg);
            }
            catch (android.os.RemoteException e1)
            {
                Log.w(getClass().getName(), "Exception sending message", e1);
            }               

我只是选择了5号,就目前而言,由于消息标识符。在这种情况下,我通过一个复杂的类JSON字符串,然后reconstrucing它的活动。

I just picked the number 5, for now, as the message identifier. In this case I'm passing a complex class in a json string and then reconstrucing it in the activity.