Android 在螢幕上列印LOG
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, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.TRANSLUCENT);
// lp.gravity=Gravity.LEFT|Gravity.TOP; //調整懸浮視窗至左上角
// 以螢幕左上角為原點,設定x、y初始化
// lp.x=0;
// lp.y=0;
final LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
listview = (ListView) inflator.inflate(R.layout.log_window, null);
logList = new LinkedList<LogLine>();
mAdapter = new LogAdapter(this, logList);
listview.setAdapter(mAdapter);
if (isAllowReadLog) {
wm.addView(listview, lp);
}
}
private void removeSystemWindow() {
if (listview != null && listview.getParent() != null) {
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeViewImmediate(listview);
}
}
class LogAdapter extends ArrayAdapter<LogLine> {
private LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public LogAdapter(Context context, List<LogLine> objects) {
super(context, 0, objects);
}
public void add(LogLine line) {
logList.add(line);
notifyDataSetChanged();
}
@Override
public LogLine getItem(int position) {
return logList.get(position);
}
@Override
public int getCount() {
return logList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LogLine line = getItem(position);
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflator.inflate(R.layout.log_line, parent, false);
holder.time = (TextView) convertView.findViewById(R.id.log_time);
holder.content = (TextView) convertView.findViewById(R.id.log_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.time.setText(line.time);
holder.content.setText(line.content);
if (line.color != 0) {
holder.content.setTextColor(line.color);
} else {
holder.content.setTextColor(getResources().getColor(android.R.color.white));
}
return convertView;
}
}
class ViewHolder {
public TextView time;
public TextView content;
}
class LogReaderThread implements Runnable {
private String filter;
public LogReaderThread(String filter) {
this.filter = filter;
}
@Override
public void run() {
Process mLogcatProc = null;
BufferedReader reader = null;
try {
mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat", filter + " *:S" });
reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
String line;
while (isAllowReadLog) {
if ((line = reader.readLine()) != null) {
Message msg = new Message();
msg.obj = line;
handler.sendMessage(msg);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void buildLogLine(String line) {
LogLine log = new LogLine();
log.time = LOGCAT_TIME_FORMAT.format(new Date()) + ": ";
if (line.startsWith("I")) {
log.color = Color.parseColor("#008f86");
} else if (line.startsWith("V")) {
log.color = Color.parseColor("#fd7c00");
} else if (line.startsWith("D")) {
log.color = Color.parseColor("#8f3aa3");
} else if (line.startsWith("E")) {
log.color = Color.parseColor("#fe2b00");
}
if (line.contains(")")) {
line = line.substring(line.indexOf(")") + 1, line.length());
}
log.content = line;
while (logList.size() > MAX_LINE) {
logList.remove();
}
mAdapter.add(log);
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
buildLogLine(msg.obj.toString());
};
};
}
//實體類
public class LogLine {
public String time;
public String content;
public int color;
}
啟動server,log將輸出到螢幕上,Android共勉!
轉自:http://blog.csdn.net/lushitianxia/article/details/51361057
相關文章
- 一個在 iOS 裝置螢幕上實時列印 Log 的小工具iOS
- android 螢幕適配Android
- android 喚醒螢幕Android
- Android螢幕適配方案Android
- Android 禁止螢幕旋轉Android
- android螢幕適配方法Android
- Android 螢幕自適應Android
- 把selection screen當子螢幕放到普通螢幕上
- Android 主流螢幕以及適配Android
- Android螢幕適配方案分析Android
- makefile&Android mk檔案中列印logAndroid
- cordova在Xcode下console.log列印XCode
- UE4-關於列印到螢幕上沒有顯示的問題
- 將Android手機的螢幕投屏到macos系統上AndroidMac
- Android 觸控式螢幕驅動Android
- Android 螢幕適配終結者Android
- Android 螢幕重新整理機制Android
- Android螢幕適配總結和思考Android
- Android技能樹 — 螢幕適配小結Android
- Android 5.0+ 螢幕錄製實現Android
- Android 突破螢幕重新整理的桎梏Android
- 為任意螢幕尺寸構建 Android 介面Android
- Android Lollipop (5.0) 螢幕錄製實現Android
- 如何在Mac上使用螢幕共享Mac
- 如何在 Mac 上錄製螢幕Mac
- Mac蓋上螢幕不休眠技巧,教你關閉螢幕不進入黑屏!Mac
- Mac蓋上螢幕後外接螢幕持續黑畫面的解決方法Mac
- Scrcpy v1.9可以在螢幕關閉的情況下映象螢幕
- Android搖一搖、螢幕方向的監聽Android
- Android螢幕適配前先了解這些Android
- android 今日頭條的螢幕適配理解Android
- Android三種方式擷取任意介面螢幕Android
- android通過MediaProjection獲取螢幕內容AndroidProject
- 教你怎麼在windows10電腦上錄製螢幕視Windows
- SideNotes 1.2.3 for Mac 讓你隨時在 Mac 螢幕上管理筆記IDEMac筆記
- WebRTC本地分享螢幕,錄製螢幕Web
- Android 螢幕適配:最全面的解決方案Android
- Log 工具列印日誌
- Android dp方式的螢幕適配工具使用(Android Studio外掛方式)Android