如何收集来自IntentService和更新Android的UI信息 [英] How to Collect info from IntentService and Update Android UI
问题描述
我学习Android和我只能和我的服务。
I'm learning Android and I'm stuck with my service.
我的应用程序通过插座连接到我的服务器每隔X秒,接收XML,解析在一个TextView的信息,并将其的表演。
My application connects via Socket to my server every X seconds, receives an XML, parses the information and it's shows in a TextView.
我想知道我怎么能实现一个IntenService做到这一点,如何沟通的信息到用户界面。我发现很难看到很好的例子。
I'd like to know how can I implement an IntenService to do this and how to communicate the info to the UI. I'm finding very hard to see good examples.
我AP preciate任何帮助,您可以给我。
I appreciate any help you can give me.
感谢您!
推荐答案
使用一个处理程序,并从intentservice发送消息给父活动
Use a handler and send a message to parent activity from the intentservice
父活动
声明处理器
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle reply = msg.getData();
// do whatever with the bundle here
}
};
调用intentservice:
Invoking the intentservice:
Intent intent = new Intent(this, IntentService1.class);
intent.putExtra("messenger", new Messenger(handler));
startService(intent);
在 IntentService
Bundle bundle = intent.getExtras();
if (bundle != null) {
Messenger messenger = (Messenger) bundle.get("messenger");
Message msg = Message.obtain();
msg.setData(data); //put the data here
try {
messenger.send(msg);
} catch (RemoteException e) {
Log.i("error", "error");
}
}
这篇关于如何收集来自IntentService和更新Android的UI信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!