sendMessage類方法, 容許你安排一個帶數據的Message對象到隊列中,等待更新. 安全
如下是一個demo: ide
1.咱們在MainActivity(主UI)線程中接受消息 oop
public class MainActivity extends Activity { post
public static MyHandler mHandler; spa
public void onCreate(Bundle savedInstanceState) { 線程
mHandler = new MyHandler(); 對象
} 隊列
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper L) {
super(L);
} 事件
@Override//接受消息
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("MyHandler", "handleMessage......");
if (msg.getData().getString("UI") == "play")
{
Log.v("MyHandler", "play!!!!!!!!!!!!!!!!!");
if (mSurfaceHolder != null)
mSurfaceHolder.setFixedSize(720, 576);
}
super.handleMessage(msg);
}
} get
}
這裏咱們在另外一個線程中發送消息:
Message msg = new Message(); Bundle b = new Bundle();// 存放數據 b.putString("UI", "stop"); msg.setData(b); MainActivity.mHandler.sendMessage(msg); // 向Handler發送消息,更新UI