HandlerThread更新ui和Log日志的用法
2015-05-22 19:30
274 查看
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);
}
}
}
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);
}
}
}
相关文章推荐
- logwatch命令_Linux logwatch 命令用法详解:可定制和可插入式的日志监视系统
- RecyclerView之更新UI数据的高级用法
- Nginx配置中的log_format用法梳理(设置详细的日志格式)
- Miranda UI Library更新日志 -> 2010-6-1: 换了一套素一些的Win7风格的墙纸
- httpUrlConnection连接网络的用法(用到了handle传递消息,在主线程中更新UI)
- log日志输出用处用法等
- UI 经常用法总结之--- UILabel UITextField (不断更新中)
- Nginx配置中的log_format用法梳理(设置详细的日志格式)
- Nginx配置中的log_format用法梳理(设置详细的日志格式)
- logrotate命令_Linux logrotate 命令用法详解:统日志进行轮转、压缩和删除
- 2048游戏回顾四:使用HandlerThread在子线程中更新UI和使用SoundPool播放音效
- Android学习笔记之:android更新ui的几种经常用法
- 关于主线程更新UI,thread、progress、dialog等的用法、
- logsave命令_Linux logsave 命令用法详解:将命令的输出信息保存到指定的日志文件
- 线程基本用法 以及在子线程中更新UI
- iOS Xcode8更新后输出log日志关闭的方法
- Android Timer 用法以及更新UI时的技巧
- android关于Handler更新UI的用法
- RecyclerView之更新UI数据的高级用法
- iOS- Xcode8更新后输出log日志关闭