下面的这段代码的执行顺序是什么?求指教。。。。。
- package com.cbt.SHDemo;
- import com.cbtService.AndroidSDK.HardwareControler;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- public class EnvActivity extends Activity {
- public static final String TAG = "EnvActivity";
- ImageView gas, body;
- Button exit;
- TextView temp_tv, hum_tv, body_tv, gas_tv;
- ProgressBar temp, hum;
- public static int hum_state;
- public static int temp_state;
- int ret, serial_fd = 0;
- private Thread mRecvThread;
- public static byte[] buf = new byte[46];
- public static byte[] buf1 = new byte[43];
- public static byte[] buf_th = new byte[43];
- public static byte[] buf_body = new byte[43];
- public static byte[] buf_gas = null;
- private final static int MSG_REFRESH = 1;
-
- @SuppressLint("HandlerLeak")
- private Handler mHandler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_REFRESH:
- refreshUI();
- default:
- break;
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_env);
- HardwareControler._init();
- serial_fd = HardwareControler.openSerialPort("/dev/s3c2410_serial2",
- 115200, 8, 1);
- init();
- }
- //init方法在onCreate中被调用
- private void init() {
- // 初始化温湿度显示组件;
- temp_tv = (TextView) findViewById(R.id.temp_tv);
- hum_tv = (TextView) findViewById(R.id.hum_tv);
- hum = (ProgressBar) findViewById(R.id.pBar_hum);
- temp = (ProgressBar) findViewById(R.id.pBar_temp);
- temp.setProgress(28);
- temp.setMax(35);
- hum.setProgress(16);
- hum.setMax(80);
- gas = (ImageView) findViewById(R.id.gas_iv);
- body = (ImageView) findViewById(R.id.body_iv);
- body_tv = (TextView) findViewById(R.id.tv_body);
- gas_tv = (TextView) findViewById(R.id.tv_gas);
- exit = (Button) findViewById(R.id.button1);
- exit.setOnClickListener(mClick);
- }
- //退出的监听mClick在init()中被调用
- private OnClickListener mClick = new OnClickListener() {
- @Override
- public void onClick(View v) {
- EnvActivity.this.finish();
- }
- };
- //getData()方法在onResume()中被调用
- public void getData() {
- mRecvThread = new Thread(new Runnable() {
- @Override
- public void run() {
- Log.d(this.toString(), Thread.currentThread().getName()
- + "---->" + "-------------===获取节点信息");
- Looper.prepare();
- while (!Thread.interrupted()) {
- ret = HardwareControler.select(serial_fd, 2, 5);
- if (ret == 1) {
- HardwareControler.read(serial_fd, buf, 1);
- if ((buf[0] & 0xFF) == 0xEE) {
- HardwareControler.read(serial_fd, buf, 1);
- if ((buf[0] & 0xFF) == 0xCC) {
- HardwareControler.read(serial_fd, buf, 1);
- if ((buf[0] & 0xFF) == 0x01) {
- HardwareControler.read(serial_fd, buf1, 43);
- if ((buf1[42] & 0xFF) == 0xFF) {
- switch (buf1[31]) {
- case 0x0B:
- buf_gas = buf1;
- System.out.printf(
- "buf_gas---------------->",
- buf_gas[39] & 0xFF);
- break;
- case 0x0A:
- buf_th = buf1;
- hum_state = (int) (((buf_th[36] & 0xFF) << 8) | (buf_th[37] & 0xFF));
- temp_state = (int) (((buf_th[38] & 0xFF) << 8) | (buf_th[39] & 0xFF));
- break;
- case 0x07:
- buf_body = buf1;
- for (int i = 0; i < buf_body.length; i++) {
- System.out
- .printf("buf_body[%d]---------------->%X\n",
- i,
- buf_body[i] & 0xFF);
- }
- break;
- }// eof-switch
- Message msg = Message.obtain();
- msg.what = MSG_REFRESH;
- mHandler.sendMessage(msg);
- }
- }// eof-buf[2]
- }// eof-buf[1]
- }// eof-buf[0]
- } else if (ret == 0) {
- System.out.println("没有接收到串口数据");
- Log.i("Java Handler", "timeout");
- } else {
- System.out.println("接收到串口数据出错");
- Log.i("Java Handler", "read error");
- }
- }// eof-while
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Looper.loop();
- }
- });
- mRecvThread.setDaemon(true);
- mRecvThread.start();
- }// eof-getData
- //refreshUI方法在mHandler对象中被调用
- private void refreshUI() {
- if (buf_th != null && buf_th[31] == 0x0A) {
- temp_tv.setText(String.valueOf((temp_state / 10.0) + "℃"));
- hum_tv.setText(String.valueOf((hum_state / 10.0) + "%"));
- temp.setProgress((temp_state / 10) - 10);
- hum.setProgress((hum_state / 10) - 10);
- }
- if (buf_body != null && buf_body[31] == 0x09) {
- switch (buf_body[39]) {
- case (byte) 0x00:
- body.setBackgroundResource(R.drawable.nousers);
- body_tv.setText("无人");
- break;
- case (byte) 0x01:
- body.setBackgroundResource(R.drawable.haveuser);
- body_tv.setText("有人");
- break;
- }
- }
- if (buf_gas != null && buf_gas[31] == 0x07) {
- switch (buf_gas[39]) {
- case (byte) 0x00:
- gas_tv.setText("正常");
- gas.setBackgroundResource(R.drawable.burnoff);
- break;
- // case (byte) 0x01:
- // gas_tv.setText("警报");
- // gas.setBackgroundResource(R.drawable.burn);
- // break;
- }
- }
- }
- @Override
- protected void onDestroy() {
- Log.d(TAG, "onDestroy");
- EnvActivity.this.finish();
- super.onDestroy();
- }
- @Override
- protected void onPause() {
- Log.d(TAG, "onPause");
- if (mRecvThread != null) {
- mRecvThread.interrupt();
- }
- super.onPause();
- }
- @Override
- protected void onRestart() {
- Log.d(TAG, "onRestart");
- // TODO Auto-generated method stub
- super.onRestart();
- }
- @Override
- protected void onResume() {
- Log.d(TAG, "onResume");
- getData();
- super.onResume();
- }
- @Override
- protected void onStart() {
- Log.d(TAG, "onStart");
- // TODO Auto-generated method stub
- super.onStart();
- }
- @Override
- protected void onStop() {
- Log.d(TAG, "onStop");
- // TODO Auto-generated method stub
- super.onStop();
- }
- }
复制代码 |