在开发中遇到已经发布到客户现场,但是出现bug,此时无法获取调试日志的问题,使用这个工具类,把日志输出到页面显示,可以协助我们快速排查问题
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.blankj.utilcode.util.Utils;
import com.imstlife.technogym.R;
/**
* 在桌面显示日志
*
* @param
* @author alias.su
* @description
* @return
* @time 2023/3/1 10:19
*/
public class WindowLogView {
private static WindowLogView instance;
private Context context;
private View layoutLog;//布局文件
private TextView logTv;
private ScrollView logScroll;
private StringBuffer logBuffer;
private WindowManager.LayoutParams params;
private WindowManager windowManager;
private boolean isShow = false;
public static boolean log_switch = false;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
dismiss();
}
};
private final int WAHT_DISMISS = 1;
private WindowLogView(Context context) {
this.context = context;
logBuffer = new StringBuffer();
initView();
}
public static WindowLogView getInstance() {
if (instance == null) {
synchronized (WindowLogView.class) {
if (instance == null) {
instance = new WindowLogView(Utils.getApp());
}
}
}
return instance;
}
private void initView() {
layoutLog = LayoutInflater.from(context).inflate(R.layout.show_log_window_layout, null);
windowManager = (WindowManager) context.getApplicationContext().getSystemService(context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
600,
500,
Build.VERSION.SDK_INT < 26 ? WindowManager.LayoutParams.TYPE_PHONE : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
logTv = layoutLog.findViewById(R.id.log_tv);
logScroll = layoutLog.findViewById(R.id.log_scroll);
if (!layoutLog.isAttachedToWindow() && !isShow) {
windowManager.addView(layoutLog, params);
}
isShow = true;
layoutLog.setVisibility(log_switch?View.VISIBLE:View.GONE);
}
public void showLog(String log) {
if (!log_switch) {
return;
}
layoutLog.setVisibility(log_switch?View.VISIBLE:View.GONE);
if (!layoutLog.isAttachedToWindow() && !isShow) {
windowManager.addView(layoutLog, params);
isShow = true;
}
String[] split = logBuffer.toString().split("\n");
if (split.length > 8) {
logBuffer.delete(0, logBuffer.length());
for (int i = 1; i < split.length; i++) {
logBuffer.append(split[i]);
logBuffer.append("\n");
}
}
logBuffer.append(log);
logBuffer.append("\n");
logTv.setText(logBuffer.toString());
logScroll.fullScroll(ScrollView.FOCUS_DOWN);
handler.removeMessages(WAHT_DISMISS);
handler.sendEmptyMessageDelayed(WAHT_DISMISS, 5000);
}
private void dismiss() {
if (layoutLog.isAttachedToWindow() && isShow) {
windowManager.removeView(layoutLog);
}
isShow = false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="500dp"
android:background="#88ffffff">
<ScrollView
android:id="@+id/log_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="@+id/log_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="输出日志"
android:textColor="#000000"
android:textSize="30dp" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
使用方法:
ThreadUtils.getInstance().executeOnMain(new Runnable() {
@Override
public void run() {
WindowLogView.getInstance().showLog("记录日志");
}
});
获取context使用到了Utilcode框架,网上集成的文章很多,百度即可。
implementation 'com.blankj:utilcodex:1.30.6'