Android消息循环
我需要将数据从类发送到主 UI 活动,并且我正在尝试通过消息传递来完成此操作。
不幸的是,我的处理程序没有收到循环内发送的消息。到目前为止,我向您展示了我的代码:
在 UI 活动中
private final Handler mIncomingHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
tedit.setText("Received " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
};
private final Messenger mMessenger = new Messenger(mIncomingHandler);
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mTransferServiceBound = true;
Message msg = Message.obtain(null, TransferService.MSG_REG_CLIENT);
msg.replyTo = mMessenger;
mTransferService = new Messenger(service);
try {
mTransferService.send(msg);
} catch (RemoteException e) {
Log.e(TAG, "Unable to register client");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mTransferService = null;
mTransferServiceBound = false;
}
};
在服务中
private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REG_CLIENT:
Log.d(TAG, "Activity client registered");
mClient = msg.replyTo;
waitCommunication();
break;
case MSG_UNREG_CLIENT:
mClient = null;
stopSelf();
break;
default:
super.handleMessage(msg);
}
}
}
final Messenger mMessenger = new Messenger(new IncomingHandler());
private void waitCommunication() {
int i = 0;
while(true) {
try {
mClient.send(Message.obtain(null, MainActivity.Message_READ, i++, -1));
} catch (RemoteException e) {
Log.e(TAG, "Unable to send Message", e);
}
}
}
当我尝试在没有 while(true) 的情况下发送消息时,它工作正常,但就像我上面描述的那样,我根本没有在处理程序上收到任何消息。
有人可以帮我解决这个问题吗?
I need to send data from a class to the main UI Activity and i am trying to do this with message passing.
Unfortunately my handler didn't receive the message sent inside a loop. I show you my code so far:
In the UI Activity
private final Handler mIncomingHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
tedit.setText("Received " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
};
private final Messenger mMessenger = new Messenger(mIncomingHandler);
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mTransferServiceBound = true;
Message msg = Message.obtain(null, TransferService.MSG_REG_CLIENT);
msg.replyTo = mMessenger;
mTransferService = new Messenger(service);
try {
mTransferService.send(msg);
} catch (RemoteException e) {
Log.e(TAG, "Unable to register client");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mTransferService = null;
mTransferServiceBound = false;
}
};
In the service
private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REG_CLIENT:
Log.d(TAG, "Activity client registered");
mClient = msg.replyTo;
waitCommunication();
break;
case MSG_UNREG_CLIENT:
mClient = null;
stopSelf();
break;
default:
super.handleMessage(msg);
}
}
}
final Messenger mMessenger = new Messenger(new IncomingHandler());
private void waitCommunication() {
int i = 0;
while(true) {
try {
mClient.send(Message.obtain(null, MainActivity.Message_READ, i++, -1));
} catch (RemoteException e) {
Log.e(TAG, "Unable to send Message", e);
}
}
}
When i try to send the message without the while(true) it works fine, but like i described above i simply didn't receive any message on the handler.
Can someone help me with this issue ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为该服务正在 UI 线程上运行。因此,如果运行无限循环,则 Activity 将永远不会获得任何 CPU 时间来响应消息。
不要执行 while (true) {...},而是分配一个 Handler。您可以使用其各种 post 方法按时间间隔重复执行操作,甚至尽可能快地执行操作,而不会完全阻塞 UI 线程上的所有其他活动。
I think the service is running on the UI thread. Thus if you have an endless loop running, the Activity will never get any CPU time to respond to the message.
Instead of doing while (true) {...}, allocate a Handler. You can use its various post methods to do things repeatedly at timed intervals, or even as fast as possible, without totally blocking all other activity on the UI thread.