在APP上显示UDP或者HTTP接收到的内容
/**
* @description: 调试用显示接收信息.
* @autour: YQQ.yang.
* @date 2017/6/12 9:19.
*/
public class LogService extends Service {
private WindowManager windowManager;
private WindowManager.LayoutParams logParams;
private TextView logTv;
private ScrollView logView;
private boolean flag = true;
private LinearLayout operateView;
private WindowManager.LayoutParams operateParams;
@SuppressLint("SimpleDateFormat")
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
private StringBuilder log = new StringBuilder("");
private static final float ALPHA = 0.6F; // window 透明度.
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
// logView 布局参数.
logParams = new WindowManager.LayoutParams();
logParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
logParams.type = WindowManager.LayoutParams.TYPE_TOAST;
logParams.gravity = Gravity.TOP;
logParams.format = PixelFormat.TRANSLUCENT;
logParams.alpha = ALPHA;
int screenHeight = windowManager.getDefaultDisplay().getHeight();
int screenWidth = windowManager.getDefaultDisplay().getWidth();
logParams.width = screenWidth;
logParams.height = (int) (screenHeight * 0.5f);
// 创建 logView 布局.
logView = new ScrollView(this);
logView.setBackgroundColor(0xFF000000);
logTv = new TextView(this);
logTv.setTextColor(0xCC00FF00);
logTv.setHint("LOG: ");
logTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
logView.addView(logTv);
// 操作栏 参数.
operateParams = new WindowManager.LayoutParams();
operateParams.type = WindowManager.LayoutParams.TYPE_TOAST;
operateParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
operateParams.format = PixelFormat.TRANSLUCENT;
operateParams.alpha = ALPHA;
operateParams.gravity = Gravity.BOTTOM;
operateParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
operateParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// 创建 操作栏布局.
operateView = new LinearLayout(this);
operateView.setOrientation(LinearLayout.HORIZONTAL);
operateView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ToggleButton toggleButton = new ToggleButton(this);
toggleButton.setOnClickListener(v -> {
if (toggleButton.isChecked()) {
windowManager.removeView(logView);
} else {
if (null == logView.getParent()) {
windowManager.addView(logView, logParams);
}
}
});
Button clearLog = new Button(this);
clearLog.setText("清除日志");
clearLog.setTextColor(0XFFFF0000);
clearLog.setOnClickListener(v -> {
log = new StringBuilder("");
logTv.setText(log);
});
Button cancelLog = new Button(this);
cancelLog.setText("关闭调试器");
cancelLog.setTextColor(0XFF00FF00);
cancelLog.setOnClickListener(v -> this.onDestroy());
operateView.addView(toggleButton);
operateView.addView(clearLog);
operateView.addView(cancelLog);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (flag) {
EventBus.getDefault().register(this);
log = new StringBuilder("");
flag = false;
if (null == logView.getParent()) {
windowManager.addView(logView, logParams);
}
if (null == operateView.getParent()) {
windowManager.addView(operateView, operateParams);
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
if (null != logView.getParent()) {
windowManager.removeView(logView);
}
if (null != operateView.getParent()) {
windowManager.removeView(operateView);
}
flag = true;
super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ReceiveMsg receiveMsg) {
String temp =
// "IP : " + receiveMsg.getIp() + " Port : " + receiveMsg.getPort() + " >>>返回数据 : \n" + (new String(receiveMsg.getMsg())) +
"Udp Info ( " + getCurrentTime() + " ) : \n"
+ "IP : " + receiveMsg.getIp()
+ " Port : " + receiveMsg.getPort()
+ " >>>返回数据 (HexStr): \n"
+ FormatUtil.byte2Hexstr(receiveMsg.getMsg());
showLog(temp);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(BaseEvents.HttpResultEvent event) {
showLog("Http Info ( " + getCurrentTime() + " ) : \n" + event.getObject().toString());
}
private void showLog(String log) {
log += "\n\n";
this.log.append(log);
logTv.setText(this.log);
}
private String getCurrentTime() {
return dateFormat.format(System.currentTimeMillis());
}
}