从服务将数据传递到活动 [英] Pass data from Service to Activity

查看:114
本文介绍了从服务将数据传递到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行本身(服务将自动启动)服务。

I have the service that runs itself (service starts automatically).

和我有活动。
本次活动按钮开始方法 DOIT()

And I have the Activity.
In this Activity button starts the method DoIt():

Button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
      DoIt();
    }
});

有些数据被写入我的服务变量数据当我按下按钮,方法的工作原理。
我可以看到数据的日志:

Some data is written to the variable data in my service when I push the button and method works.
I can see data in Log:

public String getMessage(String data) {
... 
Log.d(TAG, "Our Data: " + data);
return date;
...   

但我怎么能看到我的活动这一数据(下键),按下按钮?
谢谢你。

But how can I see this data in my Activity (under the button) by pushing the button?
Thanks.

推荐答案

您需要将服务绑定到你的活动。一旦服务绑定您可以访问你的服务的方法。看下面这个例子如何 http://developer.android.com/guide/components/bound-services.html

You need to bind the service to your activity. Once the service is bound you can access the methods in your service. Look at this example for how http://developer.android.com/guide/components/bound-services.html

您还可以将监听器添加到您的服务,让你活动将立即通知时的数据变化

You can also add a listener to your service so that you activity would be instantly notified when the data changes

这篇关于从服务将数据传递到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆