如何将数据从服务到活动的地方回调的参与? [英] How to Get data from Service to Activity where Callback is Involved?

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

问题描述

下面的情景,

  • 我想从服务到活动获得的数据

  • I want to get Data from the Service to Activity

每当服务获取新数据从服务器,下面的函数(回调)会导致自动调用

Whenever The Service gets new data from the server, the following function(callback) gets automatically called

公共无效publishArrived(等等,等等){ //一些操作在这里 }

public void publishArrived(blah, blah) { //some operation here }

  • 如何我得到上述功能回到我的活动得到的数据? (我想我应该使用使者在这里,但我不能够把握的概念)
  • How to I get the Data got from the above function back to my activity ? (I think I am supposed to use "Messenger" here but i am not able to grasp the concept)

背景:在我的活动我执行其成功取决于到达publishArrived上述结果的登录操作

CONTEXT: In my activity i Perform a Login operation whose success depends on the above result that arrives in publishArrived.

在此先感谢。

推荐答案

假设你需要开始在同一时间的活动和服务(而不是您收到的数据在服务启动后的活动),你可以注册一个BroadcastReceiver在您的活动,然后从该服务发送广播消息,一旦你拥有的数据。

Assuming that you need to start the activity and service at the same time (rather than start the activity after you receive data in the service) you can register a BroadcastReceiver in your activity, and then send a broadcast message from the service once you have the data.

在code注册在活动的广播接收机将与此类似,但你可以定义自己的自定义消息:
http://stackoverflow.com/a/2959290/483708

The code to register a broadcast receiver in your activity will be similar to this, but you'll be defining your own custom message:
http://stackoverflow.com/a/2959290/483708

要发送从你的服务,你将使用这个自定义的播放(从 HTTP解除://www.vogella.de/articles/AndroidServices/article.html ):

To send a custom broadcasts from your service you'll be using this (lifted from http://www.vogella.de/articles/AndroidServices/article.html):

Intent intent = new Intent();
intent.setAction("de.vogella.android.mybroadcast");
sendBroadcast(intent);

这篇关于如何将数据从服务到活动的地方回调的参与?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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