HandlerThread更新ui和Log日志的用法
来源:互联网 发布:java初学者论坛 编辑:程序博客网 时间:2024/06/05 08:44
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);
}
}
}
- HandlerThread更新ui和Log日志的用法
- HandlerThread 的用法 和Thread 的区别
- Handler+Thread和HandlerThread的用法比较
- Handler+Thread和HandlerThread的用法比较
- Android HandlerThread的用法
- HandlerThread的用法
- Android HandlerThread的用法
- HandlerThread的用法
- android中HandlerThread的原理和用法讲解
- Handler,looper,MessageQueue及 handlerThread 之间的关联和用法
- Android HandlerThread的简单用法
- Android线程的三种用法和子线程更新UI两种用法
- 2048游戏回顾四:使用HandlerThread在子线程中更新UI和使用SoundPool播放音效
- android关于Handler更新UI的用法
- HandlerThread的用法与自定义HandlerThread(1)
- HandlerThread的用法与自定义HandlerThread(2)
- Log日志类的开发和使用
- HandlerThread用法
- dd
- IO流第一章小结
- boostrap中文网
- UITabBarController使用详解
- openCV,C++接口,cv::Mat矩阵数据元素读取
- HandlerThread更新ui和Log日志的用法
- mysql order by的一个小坑
- [暴力]poj1166
- 企业数据备份软件
- 完美的背景图全屏css代码 – background-size:cover?
- 枚举的详细过程分析
- PHPMailer发送邮件失败:SMTP connect failed
- 桂兴堂中药生发育发简介
- c++异常