Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java打印android中进程返回的数据_Java_Android_Xml_Command Line - Fatal编程技术网

如何使用java打印android中进程返回的数据

如何使用java打印android中进程返回的数据,java,android,xml,command-line,Java,Android,Xml,Command Line,我正在尝试从my系统应用读取与系统应用关联的xml文件。我在java中使用以下代码: Process p = Runtime.getRuntime().exec("cat /data/data/app_pkg_name/shared_prefs/file.xml"); 现在据我所知,我能做到 p.getInputStream() 获取与该进程关联的输入流。如何在logcat中获取屏幕上xml的实际内容,并使用系统.out.println()打印它 当我在adb shell中的命令提示符上

我正在尝试从my系统应用读取与系统应用关联的xml文件。我在java中使用以下代码:

Process p = Runtime.getRuntime().exec("cat /data/data/app_pkg_name/shared_prefs/file.xml");
现在据我所知,我能做到

 p.getInputStream() 
获取与该进程关联的输入流。如何在logcat中获取屏幕上xml的实际内容,并使用系统.out.println()打印它

当我在adb shell中的命令提示符上执行相同的命令(第一个)时,我会在控制台上打印xml文件的内容。如何从应用程序中执行此操作


仅供参考,我的设备是根目录的,我已经在清单中使用了必要的权限。

有很多方法可以做到这一点。一种是将TextView添加到布局中,然后将其内容设置为流中的内容,例如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:orientation="vertical" >
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="" />
在Android中,不能使用system.out.println()在屏幕上输出字符串。您必须使用Android提供的功能(文本视图、对话框、祝酒词等)。请阅读有关创建UI的说明

要在logcat中打印内容,只需使用:

//declare this at the top of your activity
final static String MYTAG = "MyApp";
然后,每当您想在logcat中打印某些内容时,就使用Log.d()

Log.d(MYTAG, "Whatever content you want to put here");

使用下面的方法以字符串格式从InputStream获取数据并打印返回的字符串

private String getDataFromStream(InputStream inputStream) {
    try {
        final StringBuffer buffer = new StringBuffer();
        byte[] bs = new byte[1024];
        int read = 0;
        while ((read = inputStream.read(bs)) != -1) {
            String string = new String(bs, 0, read);
            buffer.append(string);
        }
        inputStream.close();
        return buffer.toString();
    } catch (Exception exception) {

    }
    return ""; // we got exception or no data in Stream
}

如果你想让它在设备屏幕上可见,你需要一个
TextView
并将文本放在那里。如果您
System.out.println
(或
Log.d
),它将只出现在logcat中(可以通过
adb logcat
查看)。现在,我想使用System.out.println打印。你能帮我写一段代码吗?谢谢你的回复。但我的问题是如何获取流程返回的文本,而不是在获取文本后如何显示文本。然后请重新表述您的问题,因为现在的问题意味着您在问我们如何显示文本。可能我的问题不清楚,但现在,我只想在logcat屏幕上打印我从该过程中获得的内容在设备屏幕上“在logcat屏幕上打印”是什么意思?在电脑的logcat窗口中?请澄清你想做什么。
private String getDataFromStream(InputStream inputStream) {
    try {
        final StringBuffer buffer = new StringBuffer();
        byte[] bs = new byte[1024];
        int read = 0;
        while ((read = inputStream.read(bs)) != -1) {
            String string = new String(bs, 0, read);
            buffer.append(string);
        }
        inputStream.close();
        return buffer.toString();
    } catch (Exception exception) {

    }
    return ""; // we got exception or no data in Stream
}
  /* You can use the below code to print the output using system.out.println() */


StringBuilder stringBuilder = new StringBuilder();
        try {
            String contents = "" ;
            Process p = Runtime.getRuntime().exec("cat /data/data/com.admarvel.testerofflineappv242/shared_prefs/myPrefs.xml");
            InputStream inputStream =  p.getInputStream();`enter code here`
            BufferedReader in = new BufferedReader(
                    new InputStreamReader( inputStream ) );
                while ( ( contents = in.readLine() ) != null )
                {
                    stringBuilder.append(contents);
                }
                in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("text" + stringBuilder);