`
harry
  • 浏览: 186026 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

使用MIDPLogger进行在机调试

    博客分类:
  • Java
阅读更多

在进行手机蓝牙远程控制的开发中,需要通过蓝牙控制PC,这个过程无法在模拟器中实现。因此,需要在实际手机上进行调试。比较简单的方式就是通过日志的方式,如下实现:
/*
* GUILogger.java
*
* Created on 2007年2月4日, 上午11:52
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/
package wang.mobile.guanpai;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

/**
*
* @author Xiaofeng Wang
*/
public class GUILogger implements CommandListener {
private GuanpaiGame parent;
private Form loggerForm;
private Displayable preForm;
private int level;

public final static int DEBUG = 0;
public final static int INFO = 1;

private final Command BACK_CMD = new Command("返回", Command.BACK, 0);

/**
* Creates a new instance of GUILogger
*/
public GUILogger(GuanpaiGame parent, int level) {
this.parent = parent;
this.level = level;

loggerForm = new Form("跟踪日志");
loggerForm.addCommand(BACK_CMD);
loggerForm.setCommandListener(this);
}
public void setLevel(int level) {
this.level = level;
}

public synchronized void log(String message, int level) {
if (level >= this.level) {
switch (level) {
case DEBUG:
loggerForm.append("DEBUG:" + message + "\n");
break;
case INFO:
loggerForm.append("INFO:" + message + "\n");
break;
default:
loggerForm.append("未定义的日志类型\n");
break;
}
}
}

public void showLoggerUI() {
preForm = parent.getDisplay().getCurrent();
parent.getDisplay().setCurrent(loggerForm);
}
public void commandAction(Command command, Displayable displayable) {
if (command == BACK_CMD) {
parent.getDisplay().setCurrent(preForm);
return;
}
}
}

看到Series40...上推荐的是MIDPLogger ,别人做好的包,其实自己做个也很简单。
使用也很简单:

  1. 将文件MIDPLogger,MIDPLogViewer导入你的项目
  2. 调用代码如下:

// 新建

public class MyMidlet extends MIDlet {

public static MIDPLogger logger;

protected void startApp() {

try {

logger = new MIDPLogger(MIDPLogger.DEBUG, false,true);

}catch(Exception ex) {

ex.printStackTrace();

}

protected void destroyApp(boolean unconditional) {

logger.close();
}

}

// 记录日志

MyMidlet.logger.write("log message", MIDPLogger.DEBUG);

3. 通过MIDPLogViewer,察看日志。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics