如何从 IntentService 收集信息并更新 Android UI

发布于 2024-12-11 23:01:41 字数 200 浏览 6 评论 0原文

我正在学习 Android,但我一直在使用我的服务。

我的应用程序每 X 秒通过 Socket 连接到我的服务器,接收 XML,解析信息并将其显示在 TextView 中。

我想知道如何实现 IntenService 来执行此操作以及如何将信息传递给 UI。我发现很难看到好的例子。

我很感激你能给我的任何帮助。

谢谢你!

I'm learning Android and I'm stuck with my service.

My application connects via Socket to my server every X seconds, receives an XML, parses the information and it's shows in a TextView.

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.

I appreciate any help you can give me.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2024-12-18 23:01:41

使用处理程序并从 IntentService

Parent Activity 向父 Activity 发送消息:

声明处理程序

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            Bundle reply = msg.getData();
                            // do whatever with the bundle here
            }
};

调用 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(bundle); //put the data here
        try {
            messenger.send(msg);
        } catch (RemoteException e) {
            Log.i("error", "error");
        }
    }

Use a handler and send a message to parent activity from the intentservice

Parent Activity :

Declaring Handler

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            Bundle reply = msg.getData();
                            // do whatever with the bundle here
            }
};

Invoking the intentservice:

        Intent intent = new Intent(this, IntentService1.class);
        intent.putExtra("messenger", new Messenger(handler));
        startService(intent);

Inside IntentService:

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Messenger messenger = (Messenger) bundle.get("messenger");
        Message msg = Message.obtain();
        msg.setData(bundle); //put the data here
        try {
            messenger.send(msg);
        } catch (RemoteException e) {
            Log.i("error", "error");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文