it-swarm.com.de

Eseguire il comando Shell da Android

Der Auftrag wird am Ende der Laufzeit von Dell'emulatore Dell'applicazione (im Google Play-Modus) in der App questa scrivo sue premo abgerufenenterquindi scrivi:

screenrecord --time-limit 10 /sdcard/MyVideo.mp4

e premere di nuovoenterDie Registrierung für Android KitKat wurde erfolgreich abgeschlossen.

quindi, provo a eseguire lo stesso codice da Java usando questo:

Process su = Runtime.getRuntime().exec("su");
Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4");

Ma non lavorare perché il file non è stato creato. Zum Anfang Ihr Feedback an uns Ursache und Fehlerbehebung mit Android KitKat installato. Hast du ein Problem? come posso risolvere? perché da emulatore di terminale funziona e in Java no?

36

Sie sollten sich die Standardeingabe des gerade gestarteten Prozesses su holen und den Befehl dort aufschreiben, andernfalls führen Sie die Befehle mit der aktuellen UID aus.

Versuchen Sie etwas so:

try{
    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

    outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");
    outputStream.flush();

    outputStream.writeBytes("exit\n");
    outputStream.flush();
    su.waitFor();
}catch(IOException e){
    throw new Exception(e);
}catch(InterruptedException e){
    throw new Exception(e);
}
62
Carlo Cannas

Eine Änderung des Codes durch @CarloCannas:

public static void Sudo(String...strings) {
    try{
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        for (String s : strings) {
            outputStream.writeBytes(s+"\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        outputStream.close();
    }catch(IOException e){
        e.printStackTrace();
    }
}

(Sie können gerne einen besseren Platz für outputStream.close ()) finden.

Anwendungsbeispiel:

private static void suMkdirs(String path) {
    if (!new File(path).isDirectory()) {
        Sudo("mkdir -p "+path);
    }
}

Update: Um das Ergebnis (die Ausgabe in stdout) zu erhalten, verwenden Sie:

public static String sudoForResult(String...strings) {
    String res = "";
    DataOutputStream outputStream = null;
    InputStream response = null;
    try{
        Process su = Runtime.getRuntime().exec("su");
        outputStream = new DataOutputStream(su.getOutputStream());
        response = su.getInputStream();

        for (String s : strings) {
            outputStream.writeBytes(s+"\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        res = readFully(response);
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        Closer.closeSilently(outputStream, response);
    }
    return res;
}
public static String readFully(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = is.read(buffer)) != -1) {
        baos.write(buffer, 0, length);
    }
    return baos.toString("UTF-8");
}

Das Hilfsprogramm zum automatischen Schließen einiger Closeables ( Soсket darf kein Closeable sein)

public class Closer {
// closeAll()
public static void closeSilently(Object... xs) {
    // Note: on Android API levels prior to 19 Socket does not implement Closeable
    for (Object x : xs) {
        if (x != null) {
            try {
                Log.d("closing: "+x);
                if (x instanceof Closeable) {
                    ((Closeable)x).close();
                } else if (x instanceof Socket) {
                    ((Socket)x).close();
                } else if (x instanceof DatagramSocket) {
                    ((DatagramSocket)x).close();
                } else {
                    Log.d("cannot close: "+x);
                    throw new RuntimeException("cannot close "+x);
                }
            } catch (Throwable e) {
                Log.x(e);
            }
        }
    }
}
}
26
Process p;
StringBuffer output = new StringBuffer();
try {
    p = Runtime.getRuntime().exec(params[0]);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(p.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
        output.append(line + "\n");
        p.waitFor();
    }
} 
catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}
String response = output.toString();
return response;
5
Mr.Vicky

Beispiel zum Kopieren der Datei:

void copyFile_dd(){
    try {           
        Process su;

        su = Runtime.getRuntime().exec("su");

        String cmd = "dd if=/mnt/sdcard/test.dat of=/mnt/sdcard/test1.dat \n"+ "exit\n";
        su.getOutputStream().write(cmd.getBytes());

        if ((su.waitFor() != 0)) {
            throw new SecurityException();
        }

    } catch (Exception e) {
        e.printStackTrace();
        //throw new SecurityException();
    }
}
1
ShivBuyya