【问题标题】:Android display Logcat in TextViewAndroid在TextView中显示Logcat
【发布时间】:2014-08-17 16:39:29
【问题描述】:

我正在构建的应用对 Android 具有 root 访问权限。我希望在其中一个 TextView 中显示 Logcat 日志信息,以便在我调试时它可以显示在屏幕上。

谁能给我一些想法,我可以调用哪个库/函数来访问这些日志?

谢谢

【问题讨论】:

  • 您是否有理由要自己实现 logcat-viewing 而不是使用开源应用程序alogcat
  • 主要原因是alogcat不能与应用程序并行运行。我遇到了应用程序完全使 Android Shell 无响应的问题。所以我想在我的应用程序之上实时查看发生了什么。

标签: android android-logcat android-debug


【解决方案1】:

这是一个blogpost,它完全可以满足您的需要。它有一个关于如何显示 Logcat 日志内容的完整代码示例。代码如下:

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;

  class ReadLogDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         try {
              Process process = Runtime.getRuntime().exec("logcat -d");
              BufferedReader bufferedReader = new BufferedReader(
                   new InputStreamReader(process.getInputStream()));

              StringBuilder log=new StringBuilder();
              String line = ""; 
              while ((line = bufferedReader.readLine()) != null) {
                   log.append(line);
              }   
              TextView tv = (TextView)findViewById(R.id.textView1);
              tv.setText(log.toString());
         } catch (IOException e) {
              // Handle Exception
         }
    }

  } 

澄清一下,这不是我的答案,这里是original

【讨论】:

  • 感谢您的回答。但是,我主要关心的一个问题是,我怎么知道系统中出现了新的日志行准备显示?我是否只是简单地将它们放入无限循环并不时检查它们?
  • 这是一种方法,是的,您还可以添加一个 OnClickListenear 来加载新的 Logcat 流。您提到您的应用程序使 Android Shell 无响应。你在使用 TRY/CATCH 语句吗?他们应该处理。您还可以通过 USB 电缆将手机连接到工作站,然后使用 Eclipse 并从那里使用 Logcat 视图。
  • 谢谢。我想我会在点击时触发它并每隔几秒更新一次。
  • @DawidCzerwinski 如何将结果限制为特定标签?
  • @SteveC。 stackoverflow.com/questions/6173985/… 试试这个。
【解决方案2】:

嗯,有一个解决方案可以使用this lib 记录您想要筛选的任何内容。它对我不起作用,所以我开发了自己的解决方案,您可以找到它的示例here。真的很简单,在你的项目中添加一个类 OnScreenLog

package br.com.ideiageni.onscreenlogSample;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * Created by ariel on 07/07/2016.
 */
    public class OnScreenLog {
    private static int timeoutTime = 1000;
    private static TextView tvLog;
    private static int logCount = 0;
    private static int logCountMax = 30;
    private static String[] logs = new String[logCountMax];
    private static int cntClicks = 0;
    private static boolean visibility = false;
    private static Activity activity;
    private int maxClicks = 5;

    public OnScreenLog(){}

    public OnScreenLog(Activity activity, int ViewID){
        OnScreenLog.activity = activity;
        tvLog = new TextView(activity.getApplicationContext());
        maintainLog("Log is working");
        tvLog.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
        tvLog.setTextColor(Color.BLACK);
        tvLog.setBackgroundColor(Color.LTGRAY);
        tvLog.setAlpha((float) 0.4);

        View v = null;
        LinearLayout linearLayout;
        RelativeLayout relativeLayout;
        try {
            linearLayout = (LinearLayout) activity.findViewById(ViewID);
        } catch (ClassCastException e) {linearLayout = null;};

        try {
            relativeLayout = (RelativeLayout) activity.findViewById(ViewID);
        } catch (ClassCastException e) {relativeLayout = null;};
        if(linearLayout != null) {
            linearLayout.addView(tvLog);
            v = linearLayout;
        } else if(relativeLayout != null) {
            relativeLayout.addView(tvLog);
            v = relativeLayout;
        }

        if(v != null) {
            v.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            cntClicks++;
                            timerHandler.removeCallbacks(rTimeout);
                            timerHandler.postDelayed(rTimeout, timeoutTime);

                            if (cntClicks > maxClicks-1) {
                                setLogVisible(!visibility);
                                timerHandler.removeCallbacks(rTimeout);
                                cntClicks = 0;
                            }
                            break;

                    }
                    return false;
                }
            });
        }

    }

    public void log (String text){
        String logText = text;
        maintainLog(logText);
    }

    public void log (int text){
        String logText = String.valueOf(text);
        maintainLog(logText);
    }

    public void log (int[] text){
        StringBuilder builder = new StringBuilder();
        for (int i : text) {
            builder.append(i);
            builder.append("-");
        }
        String logText = builder.toString();
        maintainLog(logText);
    }

    public void log (byte[] text){
        StringBuilder builder = new StringBuilder();
        for (int i : text) {
            builder.append(i);
            builder.append("-");
        }
        String logText = builder.toString();
        maintainLog(logText);
    }

    private void maintainLog(String newText){
        String logText = "";
        if(logCount<logCountMax) logCount++;
        for(int i=logCount-1; i>0; i--){
            logs[i] = logs[i-1];
        }
        logs[0] = newText;
        for(int i=0; i<logCount; i++){
            if(i<logCount-1) logText+=logs[i]+System.getProperty("line.separator");
            else logText+=logs[i];
        }
        tvLog.setText(logText);
    }

    public void clearLog(){
        tvLog.setText("");
    }

    public void setLogVisible(boolean visibility){
        if(visibility) tvLog.setVisibility(View.VISIBLE);
        else tvLog.setVisibility(View.INVISIBLE);
        OnScreenLog.visibility = visibility;
    }

    public static int getLogCountMax() {
        return logCountMax;
    }

    public static void setLogCountMax(int logCountMax) {
        OnScreenLog.logCountMax = logCountMax;
        logs = new String[logCountMax];
    }

    public int getMaxClicks() {
        return maxClicks;
    }

    public void setMaxClicks(int maxClicks) {
        this.maxClicks = maxClicks;
    }

    Handler timerHandler = new Handler();
    Runnable rTimeout = new Runnable() {

        @Override
        public void run() {
            cntClicks = 0;
        }
    };
}

那么,例如:

public class Activity1 extends AppCompatActivity {

private OnScreenLog log;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    log = new OnScreenLog(this, R.id.content_1);
    log.log("Started log on Activity 1");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), Activity2.class);
            startActivity(intent);
            log.log("Starting Activity 2");
            Snackbar.make(view, "Starting Activity 2", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

其中 R.id.content_1 是您的活动的主要 LinearLayout 或 RelativeLayout 的名称。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.ideiageni.onscreenlogSample.Activity1"
tools:showIn="@layout/activity_1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Activity 1!" />

</RelativeLayout>

工作尚未完成,但可供有需要的人使用。缺少一些有关如何使用的说明。欢迎提出建议。

【讨论】:

    【解决方案3】:

    如果您想稍微清理一下某个主题的上述答案的输出,那么这里有一个解决方案:

    **冒号可能不起作用,具体取决于您的日志格式**

    while ((line = bufferedReader.readLine()) != null) {
        CharSequence cs = "Mqtt:";  //topic string (the colon helps cut out false positives)
        if (line.contains(cs)) {
            log.append(line.substring(line.indexOf("Mqtt:"),line.length())); // Cuts out the junk at the beginning of the log
            log.append("\n"); // makes log print line by line like normal
        }
    }
    

    感谢@Dawid 提供了一个很好的答案!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 2015-11-18
      相关资源
      最近更新 更多