谢谢!
演示:
public class MainActivity extends Activity{private String TAG = "MainActivity";private Handler mHandler = new Handler(){ @OverrIDe public voID handleMessage(Message msg) { Log.i(TAG,"Handler:" + Thread.currentThread().getID() + "&arg1=" + msg.arg1); super.handleMessage(msg); }};@OverrIDepublic voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); Log.i(TAG,"Main:" + Thread.currentThread().getID()); testMethod();}private voID testMethod(){ Thread thread = new Thread() { @OverrIDe public voID run() { Log.i(TAG,"Thread:" + Thread.currentThread().getID()); Message msg = mHandler.obtainMessage(); msg.arg1 = 1; mHandler.dispatchMessage(msg); Message msg2 = mHandler.obtainMessage(); msg2.arg1 = 2; mHandler.sendMessage(msg2); } }; thread.start();}
}
输出:
04-19 11:32:10.452: INFO/MainActivity(774): Main:104-19 11:32:10.488: INFO/MainActivity(774): Thread:804-19 11:32:10.492: INFO/MainActivity(774): Handler:8&arg1=104-19 11:32:10.635: INFO/MainActivity(774): Handler:1&arg1=2解决方法 mHandler.dispatchMessage(msg)就像直接调用handleMessage(Message msg),我不知道什么时候会有用. Handlers的重点是能够向其他线程发送消息.这就是你用sendMessage做的事情.
编辑:你可以看到它只是为你调用handleMessage().
/** * Handle system messages here. */public voID dispatchMessage(Message msg) { if (msg.callback != null) { // callback = the Runnable you can post "instead of" Messages. msg.callback.run(); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); }}总结
以上是内存溢出为你收集整理的android – Handler.dispatchMessage(msg)和Handler.sendMessage(msg)之间的区别全部内容,希望文章能够帮你解决android – Handler.dispatchMessage(msg)和Handler.sendMessage(msg)之间的区别所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)