简体   繁体   English

如何在 Android 应用程序中获取 logcat -v 时间?

[英]How to get logcat -v time in an Android App?

I need to get logcat -v time from a device and save it to a text file in a SD Card.我需要从设备获取 logcat -v 时间并将其保存到 SD 卡中的文本文件中。 The problem is that my application freezes when I press the button to do it.问题是当我按下按钮时我的应用程序冻结了。

I understand that it might happen because logcat -v time keeps getting the log of all actions of the device, nonstop.我知道这可能会发生,因为 logcat -v time 不停地获取设备所有操作的日志。 And I need all this information.我需要所有这些信息。 But I don't know how to code it correctly.但我不知道如何正确编码。 Would anyone could help me, please?请问有人可以帮助我吗?

Thanks in advance!提前致谢!

Here is my code:这是我的代码:

btn_comeca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
try {
                    Process process = Runtime.getRuntime().exec("logcat -v");
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                    StringBuilder log=new StringBuilder();
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        log.append(line);
                    }

                    try {

                        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                            //Verifica permissão de gravar/ler
                            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                            } else {

                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MODE_ENABLE_WRITE_AHEAD_LOGGING);
                            }
                        }
                        File caminho_arquivo = new File("/sdcard/ARQUIVOFINAL.txt");
                        caminho_arquivo.createNewFile();
                        FileOutputStream fileOutputStream = new FileOutputStream(caminho_arquivo);
                        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                        TextView tv = (TextView)findViewById(R.id.texttext);
                        String texto = "";
                        //tv.setText(log.toString());
                        texto = log.toString();
                        //outputStreamWriter.append(tv.getText());
                        outputStreamWriter.append(texto);
                        outputStreamWriter.close();
                        fileOutputStream.close();
                        Toast.makeText(getBaseContext(), "Text File Saved!", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }
                catch (IOException e) {}
}

The problem may be: your logic works synchronously .问题可能是:您的逻辑是同步工作的 You should never execute work like that in main thread, since it can lead to ANR (Android Not Responding).你永远不应该在主线程中执行这样的工作,因为它会导致 ANR(Android 无响应)。

There is plenty of ways to fix that, though.不过,有很多方法可以解决这个问题。 Since you are not using frameworks for multithreading (eg RxJava, you can read about it, if you are interested), you can use AsyncTask.由于您没有使用多线程框架(例如 RxJava,您可以阅读它,如果您感兴趣的话),您可以使用 AsyncTask。 You can place this in your listener.你可以把它放在你的听众中。

EDIT : However, take a look at MVVM or MVP pattern and multithreading frameworks later.编辑:但是,稍后查看 MVVM 或 MVP 模式和多线程框架。 These things are widely used in production for executing different kinds of asynchronous tasks.这些东西在生产中被广泛用于执行不同类型的异步任务。

new AsyncTask<Void, Void, Void>(){
    @Override
    protected Void doInBackground(Void... params) {
        // Place your logic here
        return;
    }
}.execute();

Normal invocation of logcat won't every exit by itself. logcat正常调用不会每次都自行退出。

Therefore your code will be forever stuck in your while loop.因此,您的代码将永远停留在 while 循环中。

As per https://developer.android.com/studio/command-line/logcat根据https://developer.android.com/studio/command-line/logcat

Use the -d "Dump the log to the screen and exits."使用-d “将日志转储到屏幕并退出”。 commandline option.命令行选项。

This is what I use in a similar method to display the logcat output in to a TextView这是我在类似方法中使用的将 logcat 输出显示到TextView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM