我正在使用线程来执行一些 BT 任务。我正在尝试向 UI 线程发送消息,以便我可以基于我的 BT 线程执行 UI 工作。为此,我使用处理程序,但我不知道如何检索发送到处理程序的数据。
要发送数据,我使用:
handler.obtainMessage(intCode).sendToTarget();
其中 intCode 是一个 int。我的处理程序看起来像这样。
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
Bundle data = msg.getData();
int code = data.getInt("what");
Log.d(LOG_TAG, "Msg: "+code);
}
}
但 code 的值绝不会是 0 以外的任何值。在执行 .obtainMessage(int) 时如何获取发送的值?该值是否未存储在带有“what”键的 Bundle 中?
您可以按此格式设置数据
设定数据时
Message msg = new Message();
msg.obj = data which you want to set // for object data
Msg.arg1 = data which you want to set // for integer data
获取数据的同时
String data = (String) msg.obj; // If object is of String
int integerData = msg.arg1;
msg.arg1 一次仅传递一个数据,您也可以在 msg.arg2 中传递数据,它们都是整数类型
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)