Android异步消息处理实例
2017-03-30 17:32
155 查看
憋说话,看代码
package xxxxxx import android.os.HandlerThread; import android.os.Looper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.view.View; import android.util.Log; import android.os.Handler;; import android.os.Message; public class MainActivity extends AppCompatActivity { private Button mButton; private final String TAG="MessageTest"; private int ButtonCount = 0; private Thread myThread; private MyThread myThread2; private Handler mHandler; private Handler mHandler3; private int mMessageCount = 0; private HandlerThread myThread3; class MyRunnable implements Runnable{ public void run(){ int count = 0; for(;;){ Log.d(TAG, "Mythread "+count); count++; try{ Thread.sleep(3000); }catch (InterruptedException e){ e.printStackTrace(); } } } } class MyThread extends Thread{ private Looper mLooper; public void run(){ super.run(); Looper.prepare(); synchronized (this){ mLooper = Looper.myLooper(); notifyAll(); } Looper.loop(); } public Looper getLooper(){ if(!isAlive()){ return null; } synchronized (this){ while(isAlive() && mLooper == null){ try{ wait(); }catch (InterruptedException e){ } } } return mLooper; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button)findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click Log.d(TAG, "Send Message "+ ButtonCount); ButtonCount++; Message msg = new Message(); mHandler.sendMessage(msg); mHandler3.post(new Runnable() { @Override public void run() { Log.d(TAG, "get Message for Thread3 "+ mMessageCount); mMessageCount++; } }); } }); myThread = new Thread(new MyRunnable(),"MessageTestThread"); myThread.start(); myThread2 = new MyThread(); myThread2.start(); mHandler = new Handler(myThread2.getLooper(),new Handler.Callback(){ public boolean handleMessage(Message msg){ Log.d(TAG,"get Message " + mMessageCount); mMessageCount++; return false; } }); myThread3 = new HandlerThread("MessageTestThread3"); myThread3.start(); mHandler3 = new Handler(myThread3.getLooper()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
相关文章推荐
- Android异步消息处理实例
- Android异步消息处理实例
- Android异步消息处理实例
- Android异步消息处理实例
- Android异步消息处理实例
- Android异步消息处理实例
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- Android异步消息处理之Thread+Handler
- Android FrameWork 之异步消息处理线程
- Android异步消息处理
- Android之异步处理Handler和消息机制处理
- android HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理(2)
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- Android消息队列(二)--异步消息处理
- 【转】Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- (一)Android 异步消息处理
- 26、Android之Handler异步消息处理机制
- android之handler使用--对于异步消息的处理
- [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解