• Android开发——消息处理传递机制


    在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间。在Android中,默认情况下,所有的操作都是在主线程中进行的,主线程负责与UI相关的事件。而在自己新建的线程中,不能对UI进行操作。因此Android提供了消息处理传递机制来解决这一问题。

    Message,消息类。存放于MessageQueue中,包含数据类型,用户自定义的消息代码等。

    MessageQueue,消息队列。在MessageQueue中,存放的消息按照FIFO(先进先出)的原则执行。

    Handler,消息发送类。发送或者处理Message对象到所在线程的MessageQueue中。

    Looper,循环者。用来循环读取存放于MessageQueue中的消息。一个线程对应一个Looper,一个Looper对象对应一个MessageQueue。Android中新增的线程是没有开启消息循环的,但是主线程除外。系统自动为主线程创建Looper对象。

    一:在非主线程中创建Looper

    class LooperThread extends Thread {
          public Handler mHandler;//声明一个Handler对象
             
          public void run() {
              Looper.prepare(); //初始化Looper对象         
              mHandler = new Handler() {
                  public void handleMessage(Message msg) { //重写方法
       
      // process incoming messages here
                  }
              };
              Message m=mHandler.obtainMessage();//获取一个消息
              m.what=0x11;设置Message的what属性的值
              mHandler.sendMessage();//发送消息         
              Looper.loop();//启动Looper
          }
    }

    二:一个打地鼠游戏

    public class MainActivity extends Activity {
      private int i = 0; // 记录其打到了几只地鼠
      private ImageView mouse; // 声明一个ImageView对象
      private Handler handler; // 声明一个Handler对象
      public int[][] position = new int[][] { { 231, 325 }, { 424, 349 },
          { 521, 256 }, { 543, 296 }, { 719, 245 }, { 832, 292 },
          { 772, 358 } }; // 创建一个表示地鼠位置的数组
       
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mouse = (ImageView) findViewById(R.id.imageView1); // 获取ImageView对象
        mouse.setOnTouchListener(new OnTouchListener() {
       
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            v.setVisibility(View.INVISIBLE); // 设置地鼠不显示
            i++;
            Toast.makeText(MainActivity.this, "打到[ " + i + " ]只地鼠!",
                Toast.LENGTH_SHORT).show(); // 显示消息提示框
            return false;
          }
        });
       
        handler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
            int index = 0;
            if (msg.what == 0x101) {
              index = msg.arg1; // 获取位置索引值
              mouse.setX(position[index][0]); // 设置X轴位置
              mouse.setY(position[index][1]); // 设置Y轴位置
              mouse.setVisibility(View.VISIBLE); // 设置地鼠显示
            }
            super.handleMessage(msg);
          }
       
        };
        Thread t = new Thread(new Runnable() {
       
          @Override
          public void run() {
            int index = 0; // 创建一个记录地鼠位置的索引值
            while (!Thread.currentThread().isInterrupted()) {
              index = new Random().nextInt(position.length); // 产生一个随机数
              Message m = handler.obtainMessage(); // 获取一个Message
              m.what = 0x101; // 设置消息标识
              m.arg1 = index; // 保存地鼠标位置的索引值
              handler.sendMessage(m); // 发送消息
       
              try {
                Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段时间
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
       
            }
       
          }
       
        });
        t.start(); // 开启线程
       
      }
       
    }
  • 相关阅读:
    Treap 树堆 容易实现的平衡树
    (转)Maven实战(二)构建简单Maven项目
    (转)Maven实战(一)安装与配置
    根据请求头跳转判断Android&iOS
    (转)苹果消息推送服务器 php 证书生成
    (转)How to renew your Apple Push Notification Push SSL Certificate
    (转)How to build an Apple Push Notification provider server (tutorial)
    (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
  • 原文地址:https://www.cnblogs.com/a354823200/p/4048495.html
Copyright © 2020-2023  润新知