问题描述
我创建了同步数据在网络上的一个后台线程,并希望在该服务已完成,因此它可以更新它的指针通知列表活动服务?什么是做到这一点的最好方法是什么?我想发送一个广播的服务做,但不知道这是做到这一点的最好办法时。我需要重新查询光标时,该服务已经完成,所以我不知道这会工作以及与广播接收机?我没有提前做了很多的Android在一段时间这样的感谢。
I created a service which syncs data from the web on a background thread and want to notify a list activity when the service has completed so it can update it's cursor? What would be the best way to do this? I'm thinking of sending a broadcast when the service is done but not sure if that's the best way to do it. I need to requery the cursor when the service has finished so I'm not sure if that will work well with broadcast receivers? I haven't done alot of android in awhile so thanks in advance.
推荐答案
使用处理程序
在你的服务,注册了一个客户端,当你的 ListActivity
连接到服务;也就是说,在你的 ListActivity
,发送的留言
为您服务,让您跟踪连接的客户端。然后,你可以简单地通过 onServiceConnected
方法服务于这些客户端的环
,并给他们发送消息
当事情发生在你的服务,你要通知你的
。欲了解更多信息,你可以看看code。在我的一个正在进行的项目:我的<$c$c>ListActivity和我的<$c$c>Service存根。 ListActivity
关于
Use a Handler
in your service that registers a client when your ListActivity
connects to the service; that is, in your onServiceConnected
method in your ListActivity
, send a Message
to your service that enables you to keep track of connected clients. Then you can simply loop through these clients in your Service
and send them a Message
when something takes place in your Service
that you want to notify your ListActivity
about. For more information you can look at code in an on-going project of mine: my ListActivity
and my Service
stub.
总之,在你的 MainActivity
,启动并绑定到你的服务有:
In short, in your MainActivity
, start and bind to your service with:
Intent i = new Intent(this, NetworkService.class);
startService(i);
bindService(i, networkServiceConnection, Context.BIND_AUTO_CREATE);
定义使者从服务响应消息,如:
Define a messenger to respond to messages from the service like:
Messenger messenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case NetworkService.MSG_SOMETHING:
// do something here
break;
default:
super.handleMessage(msg);
}
}
}
,然后写你的服务连接code这样的:
And then write your service connection code like:
private ServiceConnection networkServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
networkService = new Messenger(service);
try {
Message msg = Message.obtain(null, NetworkService.MSG_REGISTER_CLIENT);
msg.replyTo = messenger;
networkService.send(msg);
log.debug("Connected to service");
} catch (RemoteException e) {
// Here, the service has crashed even before we were able to connect
}
}
请注意,的replyTo
是我们刚创建的使者。
Note that the replyTo
is the messenger we just created.
在你的网络服务
,跟踪连接的客户端有:
In your NetworkService
, keep track of connected clients with:
ArrayList<Messenger> clients = new ArrayList<Messenger>();
和创建的处理程序,如:
and create your handler like:
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REGISTER_CLIENT:
log.debug("Adding client: " + msg.replyTo);
clients.add(msg.replyTo);
break;
default:
super.handleMessage(msg);
break;
}
}
}
然后,当你想要发送一条信息给你的 MainActivity
,只是做类似如下:
Then, when you want to send a message back to your MainActivity
, just do something like the following:
for (int i = 0; i < clients.size(); i++) {
try {
clients.get(i).send(Message.obtain(null, MSG_SOMETHING));
} catch (RemoteException e) {
// If we get here, the client is dead, and we should remove it from the list
log.debug("Removing client: " + clients.get(i));
clients.remove(i);
}
}
这篇关于Android的服务通知活动已完成最好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!