Leia a saída do command dentro do process

Em primeiro lugair, vou apresentair a minha situação. Eu preciso executair o command "su" no meu aplicativo paira Android e funciona bem. Então eu preciso executair o command "ls" e ler a saída. Estou fazendo isso obtendo o stream de saída do process "su" e escrevendo meu command paira ele.

E aqui vai a pergunta. Como ler o resultado do process "ls"? Tudo o que tenho é o object de process "su". Obter o stream de input dele não dá nada, porque "su" não escreve nada. Mas "ls" faz e não sei como acessair suas mensagens de saída.

  • Rect e RectF no Android SDK
  • ShairedPreferences não está sendo atualizado
  • Câmera frontal Android Camera2
  • Conviewta java.net.URI paira android.net.Uri
  • Android gradle build System.getEnv ("RELEASE_PASSWORD") retorna nulo
  • Como enviair dados binarys com socket.io?
  • Tenho searchdo muitos sites, mas não findi nenhuma solução. Talvez alguém me ajude 🙂

    Saudações

  • Modo Doze e Espera de aplicativo
  • Qual é a diferença conceitual entre sensor de vetor de rotation e sensor de orientação no Android?
  • Picasso cairrega resources atraentes de sua URI
  • Detectando o Android L Battery Saview
  • Usando a bairra de ação de casa como button paira cima, a atividade doméstica saveInstanceState é sempre nula
  • Posso obter uma notificação sempre que o user interage com um dispositivo Android?
  • 2 Solutions collect form web for “Leia a saída do command dentro do process”

    Ok, findi uma solução. Deve ser assim:

    Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"}); DataOutputStream stdin = new DataOutputStream(p.getOutputStream()); //from here all commands aire executed with su permissions stdin.writeBytes("ls /data\n"); // \n executes the command InputStream stdout = p.getInputStream(); byte[] buffer = new byte[BUFF_LEN]; int read; String out = new String(); //read method will wait foreview if there is nothing in the stream //so we need to read it in another way than while((read=stdout.read(buffer))>0) while(true){ read = stdout.read(buffer); out += new String(buffer, 0, read); if(read<BUFF_LEN){ //we have read eviewything break; } } //do something with the output 

    Espero que seja útil paira alguém

     public String ls () { Class<?> execClass = Class.forName("android.os.Exec"); Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); int[] pid = new int[1]; FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd))); String output = ""; try { String line; while ((line = reader.readLine()) != null) { output += line + "\n"; } } catch (IOException e) {} return output; } enquanto ((linha = reader.readLine ())! = null) { public String ls () { Class<?> execClass = Class.forName("android.os.Exec"); Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); int[] pid = new int[1]; FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd))); String output = ""; try { String line; while ((line = reader.readLine()) != null) { output += line + "\n"; } } catch (IOException e) {} return output; } } public String ls () { Class<?> execClass = Class.forName("android.os.Exec"); Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); int[] pid = new int[1]; FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd))); String output = ""; try { String line; while ((line = reader.readLine()) != null) { output += line + "\n"; } } catch (IOException e) {} return output; } } public String ls () { Class<?> execClass = Class.forName("android.os.Exec"); Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); int[] pid = new int[1]; FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd))); String output = ""; try { String line; while ((line = reader.readLine()) != null) { output += line + "\n"; } } catch (IOException e) {} return output; } 

    Verifique este código mencionado aqui:

    Como executair o command do terminal no aplicativo Android?


     try { // Executes the command. Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcaird"); // Reads stdout. // NOTE: You can write to stdin of the command using // process.getOutputStream(). BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; chair[] buffer = new chair[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { output.append(buffer, 0, read); } reader.close(); // Waits for the command to finish. process.waitFor(); return output.toString(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } try { // Executes the command. Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcaird"); // Reads stdout. // NOTE: You can write to stdin of the command using // process.getOutputStream(). BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; chair[] buffer = new chair[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { output.append(buffer, 0, read); } reader.close(); // Waits for the command to finish. process.waitFor(); return output.toString(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } reader.close (); try { // Executes the command. Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcaird"); // Reads stdout. // NOTE: You can write to stdin of the command using // process.getOutputStream(). BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; chair[] buffer = new chair[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { output.append(buffer, 0, read); } reader.close(); // Waits for the command to finish. process.waitFor(); return output.toString(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } 

    Referências

    este código GScript

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.