从 Android 程序运行 shell 命令

发布于 2024-12-05 06:56:59 字数 1726 浏览 0 评论 0原文

这个问题以前曾在此处提出过,但是所提供的解决方案不起作用。.我正在尝试显示 /data /dalvik-cache文件夹的内容。我知道要这样做,我们需要成为SU。我什至这样做了,但我仍然无法执行shell命令。

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命令。我正在模拟器中测试该应用程序,该应用程序默认存在

This question has been asked here before but the solutions provided are not working..I am trying to display the contents of /data/dalvik-cache folder. I know that to do this we need to become su. I even did that but still i am unable to execute a shell command..

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);
        }
    }

}

Can anyone please help me out in finding out how to run linux commands in android application. I am testing this app in my emulator which is defaultly rooted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆で 2024-12-12 06:56:59

您不能简单地在模拟器上运行“ su”,默认情况下没有根访问。您需要安装“ su”程序以及superuser.apk,除非使用快照,否则每次启动模拟器时都必须执行此操作。

可以找到更多信息和链接到您需要的文件在这里< /a>以及此博客文章罗素·戴维斯(Russell Davis)

You can't simply run 'su' on the emulator, there's no root access by default. You'll need to install the 'su' program as well as the SuperUser.apk, and you'll have to do this each time you start the emulator unless using snapshots.

More information and links to the files you need can be found here on SO as well as this blog post by Russell Davis

触ぅ动初心 2024-12-12 06:56:59

我认为问题在于您正在使用两个不同的流程实例。
您必须位于 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();
}

I think the problem comes from the fact that you are using TWO different process instances.
You have to be on the su process to carry on sending commands:

You can check the question "Read command output inside su process"
for an answer.

Then I tried & managed to make working code (I'm sure it works!)

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();
}
独行侠 2024-12-12 06:56:59

在下面的例子中,我尝试执行“/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();

希望这会有所帮助。

In the below example, I try to execute "/system/bin/screencap" to capture android screen.

via adb:

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

via Android app:

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();

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文