2

Xlocation で Android デバイスにあるものを特定できるようにする必要があるプログラムに取り組んでいます。「su ls Xlocation」を使用しています

ファイルの配列リストを取得したいのですが、最初の項目しか取得できません。次の行を取得するコマンドがありませんか? または、他に何かする必要がありますか。

以下は私が送信している私のコマンドです

String[] commands = new String[]{"ls /system/app/"};
return doCommand(commands);

以下はdoCommandの現在の方法です

    private boolean doCommand(String[] commands)
    {
    ArrayList<String> output = new ArrayList<String>();

    boolean ran = false;
    try
    {
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        DataInputStream ins = new DataInputStream(process.getInputStream());

        // This part works I sends the command right!
        for (String single : commands) 
        {
            os.writeBytes(single + "\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            ran = true;
        }

        int av = -1;
        while (av != 0)
        {

//////////////////////////// WORKING ON THIS TO GET ALL INFO /////////////////
            av = ins.available();
            if (av != 0) 
            {
                byte[] b = new byte[av];
                ins.read(b);
                output.add(new String(b));
                System.out.println(new String(b) + "Recieved form modem");
            }
        }
    }
    catch(Exception ex){}
    return ran;
}

現在見られるように、true または false のみを返します。ただし、デバッグで実行すると、出力の最初の項目しか取得できません。(出力 = "[first.apk")

新しいバージョンを編集。

    public ArrayList<String> doCommand(String[] commands)
{

    ArrayList<String> output = new ArrayList<String>();

    try
    {

        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());  
        DataInputStream ins = new DataInputStream(process.getInputStream());
        for (String single : commands) 
        {
            os.writeBytes(single + "\n");

            //os.flush();
            output.add("true");
        }
        os.writeBytes("exit\n");


        byte[] bc = new byte[10000];
        String st = "";
        while(ins.read(bc) != -1)
        {
        st += new String(bc);
        os.flush();

        }
        output.add(st);


        os.flush();
        os.close();
        ins.close();
        process.waitFor();          
    }
    catch(Exception ex)
    {}

    return output;
}

現在、かなりの量の出力が得られていますが、チェックしたバイト[10000]のサイズ制限内の大きなアイテムがディレクトリにあるすべてではありません。

誰かがこれを改善して、うまくいく正確な答えを得たいと思っているなら、私はまだこの投稿をチェックしています.

ありがとう

4

2 に答える 2

2

私のオープンソースのユーザー管理(GitHub)アプリからこの方法を採用して、やりたいことを実行してみてください。これにより、ターミナルコマンドに続く出力のすべての行が読み取られます。

public static String[] getUserList()
{
    Process p;
    try {
        p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());  
        BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));

            os.writeBytes("pm list-users"+"\n");
            os.writeBytes("exit\n"); 
            ArrayList<String> users = new ArrayList<String>();
            String test;
            bf.readLine();
            while((test = bf.readLine()) != null)
            {
                users.add(test);
            }

            String[] userList = (String[]) users.toArray(new String[users.size()]);



        os.flush();
        return userList;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
于 2013-01-10T20:53:59.910 に答える