Android为了线程安全,只允许在主线程中修改UI,如果想在子线程中更改UI,可以使用Handler.
用法一:
//在子线程中发送更新信息 Message msg = Message.obtain(); msg.what = 1; Bundle data = new Bundle(); data.putString("sourceStr", result); data.putString("targetStr", result); msg.setData(data); handler.sendMessage(msg);
//在主线程中处理信息 handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { strTV.setText(msg.getData().getString("sourceStr")); showTV.setText(msg.getData().getString("targetStr")); } } };
new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { JSONObject object = (JSONObject) ((Object) tempStr); SrcID srcID = (SrcID) object.opt("result"); showTV.setText(srcID.getArea()); } });
原文地址:http://blog.csdn.net/pengkv/article/details/42110667