Android学习笔记-Handler的使用
成都创新互联公司是一家集网站建设,白山企业网站建设,白山品牌网站建设,网站定制,白山网站建设报价,网络营销,网络优化,白山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
点击Start按钮,每隔3秒在Logcat里打印一句话
MainActivity.java
public class MainActivity extends ActionBarActivity { private Button startButton = null; private Button endButton = null; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.id.startButton); endButton = (Button) findViewById(R.id.endButton); startButton.setOnClickListener(new StartButtonListener()); endButton.setOnClickListener(new EndButtonListener()); } class StartButtonListener implements OnClickListener{ @Override public void onClick(View v) { handler.post(updateThread); } } class EndButtonListener implements OnClickListener{ @Override public void onClick(View v) { handler.removeCallbacks(updateThread); } } Runnable updateThread = new Runnable() { @Override public void run() { System.out.println("updateThread"); handler.postDelayed(updateThread, 3000); } }; }
点击Start按钮之后每隔11000毫秒进度条增加10%
界面 activity_main.xml
MainActivity.java
public class MainActivity extends ActionBarActivity { private ProgressBar bar = null; private Button startButton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bar = (ProgressBar) findViewById(R.id.bar); startButton = (Button) findViewById(R.id.startButton); startButton.setOnClickListener(new ButtonListener()); } //当点击StartButton按钮时就会执行ButtonListener的onClick方法 class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { System.out.println("Onclick"); Toast.makeText(MainActivity.this, "Onclick", Toast.LENGTH_SHORT).show(); bar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } //使用匿名内部类来复写Handler当中的handleMessage方法 Handler updateBarHandler = new Handler(){ public void handleMessage(android.os.Message msg) { bar.setProgress(msg.arg1); updateBarHandler.post(updateThread); }; }; //线程类,使用匿名内部类的方式进行声明 Runnable updateThread = new Runnable() { int i = 0; @Override public void run() { System.out.println("Begin Thread"); i = i + 10; //得到一个消息对象。Message类是由Android操作系统提供。 Message msg = updateBarHandler.obtainMessage(); //使用arg1和arg2这两个成员变量传递消息,优点是节省系统资源 msg.arg1 = i; try { //设置当前线程睡眠一秒 Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } //将msg加入到消息队列当中 updateBarHandler.sendMessage(msg); if (i== 100) { System.out.println(" i ==100"); updateBarHandler.removeCallbacks(updateThread); } } }; }
可以看到处理消息的线程和主线程是同一个线程
public class MainActivity extends ActionBarActivity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler.post(runnable); /*如果采用这种方式则会创建新的线程 Thread thread = new Thread(runnable); thread.start(); */ setContentView(R.layout.activity_main);// 要等10秒钟才显示 System.out.println("Activity:" + Thread.currentThread().getId()); System.out.println("ActivityName:" + Thread.currentThread().getName()); } // 上面打印的信息和下面打印的信息是一样的 // Handler处理线程的时候并没有创建新的线程 Runnable runnable = new Runnable() { @Override public void run() { System.out.println("Handler:" + Thread.currentThread().getId()); System.out.println("HandlerName:" + Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (Exception e) { } } }; }
在新的线程中处理消息,真正实现异步消息处理
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //打印当前线程的ID System.out.println("MainActivity>>>" + Thread.currentThread().getId()); //生成一个HandlerThread对象,实现了使用Looper来处理消息队列的功能 HandlerThread handlerThread = new HandlerThread("handler_Thread"); handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); //msg.obj = "abc"; //简单数据 Bundle bundle = new Bundle(); bundle.putInt("age", 20); bundle.putString("name", "umgsai"); msg.setData(bundle); //将msg发送到目标对象,即生成msg对象的Handler对象 msg.sendToTarget(); } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); //String s = (String) msg.obj; //取出数据 Bundle bundle = msg.getData(); System.out.println("age:" + bundle.getInt("age") + ",name:" + bundle.getString("name")); System.out.println("Handler>>>" + Thread.currentThread().getId()); System.out.println("handlerMeaage"); } } }
本文标题:Android学习笔记-Handler的使用
转载来于:http://scyingshan.cn/article/pcceec.html