Android开发中需要迅速定位问题,在Android 屏幕上打印LOG,是一个很好的通道
基本的思路:启动LogService读取指定log,使用WindowManager展示到屏幕上
直接上代码
public class LogService extends Service {
private ListView listview;
private LinkedList<LogLine> logList = new LinkedList<LogLine>();
private LogAdapter mAdapter;
private final int MAX_LINE = 500;
private SimpleDateFormat LOGCAT_TIME_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");
private Thread readLog;
private boolean isAllowReadLog = false;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Utility.LOG_TAG 为自定义的logString,service会读取此log
readLog = new Thread(new LogReaderThread(Utility.LOG_TAG));
readLog.start();
createSystemWindow();
isAllowReadLog = true;
return START_STICKY;
}
@Override
public void onDestroy() {
removeSystemWindow();
isAllowReadLog = false;
super.onDestroy();
}
private void createSystemWindow() {
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGr