1. 程式人生 > >HandlerThread更新ui和Log日誌的用法

HandlerThread更新ui和Log日誌的用法

1、HandlerThread 用法例項

package com.example.handledemos;


import com.example.handledemos.util.LogUtil;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * 主執行緒與子執行緒之間的資訊互動
 * @author lzl
 *用到HandlerThread方法,主要解決多執行緒併發問題。
 */
public class HandlerThreadActivity  extends Activity implements OnClickListener{
    //建立主執行緒中的handler
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Message message = new Message();
            //handlerthread每隔1秒向子執行緒傳送一次訊息。
            handlerthread.sendMessageDelayed(message, 1000);
            LogUtil.i("主執行緒", "Main send the message");
        };
    };
    
    private Handler handlerthread;
    private HandlerThread thread;
    private Button button1;
    private Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handlerthread);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        
        thread = new HandlerThread("threadHandler");
        thread.start();
        handlerthread = new Handler(thread.getLooper()){
            @Override
            public void handleMessage(Message msg) {
                Message message = new Message();
                //handler每隔1秒向主執行緒傳送一次訊息。
                handler.sendMessageDelayed(message, 1000);
                LogUtil.i("子執行緒", "Thread send the message");
            }
        };
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            handler.sendEmptyMessage(1);  
            break;
        case R.id.button2:
            handler.removeMessages(0);    //傳入0時會停止傳送訊息。
            break;
        default:
            break;
        }
    }
}

2、Log控制列印,在完成大型專案後,執行程式是還會列印Log資訊,程式設計師不可能將其逐個刪除掉。因此可以寫個Util包,來控制Log日誌的列印。

當完成工程後,就不用擔心這個問題了。

package com.example.handledemos.util;

import android.util.Log;

public class LogUtil {
    public static final int VERBOSE = 1;
    public static final int DEBUG = 2;
    public static final int INFO = 3;
    public static final int WARN = 4;
    public static final int ERROR = 5;
    public static final int NOTHING = 6;
    public static final int LEVEL = NOTHING;
    
    public static void v(String tag , String msg){
        if(LEVEL <= VERBOSE){
            Log.v(tag, msg);
        }
    }
    public static void d(String tag , String msg){
        if(LEVEL <= DEBUG){
            Log.d(tag, msg);
        }
    }
    public static void i(String tag , String msg){
        if(LEVEL <= INFO){
            Log.i(tag, msg);
        }
    }
    public static void w(String tag , String msg){
        if(LEVEL <= WARN){
            Log.w(tag, msg);
        }
    }
    public static void e(String tag , String msg){
        if(LEVEL <= ERROR){
            Log.e(tag, msg);
        }
    }
}