5

この質問は以前にここで尋ねられましたが、提供された解決策が機能していません。/data/dalvik-cacheフォルダーの内容を表示しようとしています。これを行うには、suになる必要があることを私は知っています。私もそうしましたが、それでもシェルコマンドを実行できません。

package org.linuxconfidg.Example2;

import android.app.Activity;
import android.widget.*;
import android.os.Bundle;
import java.io.*;
public class Example2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String lsreturn=myFunLs();
        TextView tv=new TextView(this);
        tv.setText("Hello Sindhu !! Try to get it \n"+lsreturn);
        setContentView(tv);
    }

    public String myFunLs()
    {

        try {
            // Executes the command.
            Process process;
            process = Runtime.getRuntime().exec("/system/bin/su");
            process = Runtime.getRuntime().exec("/system/bin/ls /data/dalvik-cache > /data/local");
            pr
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[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);
        }
    }

}

誰かがAndroidアプリケーションでLinuxコマンドを実行する方法を見つけるのを手伝ってくれませんか?デフォルトでルート化されているエミュレーターでこのアプリをテストしています

4

3 に答える 3

3

エミュレーターで単純に「su」を実行することはできません。デフォルトではルート アクセスはありません。「su」プログラムと SuperUser.apk をインストールする必要があります。スナップショットを使用しない限り、エミュレータを起動するたびにこれを行う必要があります。

必要なファイルへの詳細情報とリンクは、SO のこちら、およびRussell Davis によるこのブログ投稿にあります。

于 2011-09-17T04:46:33.200 に答える
3

問題は、2 つの異なるプロセス インスタンスを使用しているという事実にあると思います。suコマンドの送信を続行するには、プロセスを実行する必要があります。

回答については、 「su プロセス内のコマンド出力を読み取る」という質問を確認でき ます。

次に、動作するコードを作成しようとしました(動作すると確信しています!)

public void runAsRoot(String[] cmds) throws Exception {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    InputStream is = p.getInputStream();
    for (String tmpCmd : cmds) {
        os.writeBytes(tmpCmd+"\n");
        int readed = 0;
        byte[] buff = new byte[4096];

        // if cmd requires an output
        // due to the blocking behaviour of read(...)
        boolean cmdRequiresAnOutput = true;
        if (cmdRequiresAnOutput) {
            while( is.available() <= 0) {
                try { Thread.sleep(200); } catch(Exception ex) {}
            }

            while( is.available() > 0) {
                readed = is.read(buff);
                if ( readed <= 0 ) break;
                String seg = new String(buff,0,readed);
                console.println("#> "+seg);
            }
        }
    }        
    os.writeBytes("exit\n");
    os.flush();
}
于 2012-06-29T17:04:05.260 に答える
1

以下の例では、「/system/bin/screencap」を実行して Android 画面をキャプチャしようとしています。

adb経由:

> adb shell
# /system/bin/screencap -p /sdcard/myscreenshot.png

Android アプリ経由:

sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + path).getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();

お役に立てれば。

于 2012-10-10T03:56:45.600 に答える