如何运行简单的`cd`命令

时间:2014-10-11 08:39:39

标签: android process runtime.exec

我知道有很多关于从android运行bash命令的问题。但我没有发现任何有用的东西。

有人可以向我解释public Process exec (String[] progArray, String[] envp, File directory)的论点。我知道它们已在http://developer.android.com/上解释,但我仍然无法理解envp的目的。

我使用以下代码执行命令:

例如command = "cd ./Alarms";请参阅image

public String execute(String command) {
    //String cmd[] = command.split(" ");        //DEV
    StringBuffer output = new StringBuffer();
    Process p;
    String response = null;
    try {
        p = Runtime.getRuntime().exec("/system/bin/"+command, null, Environment.getExternalStorageDirectory());
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }
        response = output.toString();
    } catch (Exception e) {
        response = e.getMessage();
        e.printStackTrace();
    }
    return response;
}

我在我的设备和模拟器上测试了这个程序并获得了相同的输出。

10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ java.io.IOException: Error running exec(). Command: [/system/bin/cd, ./Alarms] Working Directory: /storage/emulated/0 Environment: null
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.ProcessManager.exec(ProcessManager.java:211)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.Runtime.exec(Runtime.java:173)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.Runtime.exec(Runtime.java:246)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.smst.dotmav.smst.TerminalActivity.execute1(TerminalActivity.java:114)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.smst.dotmav.smst.TerminalActivity$1.onKey(TerminalActivity.java:56)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.View.dispatchKeyEvent(View.java:7660)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:358)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3845)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3819)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3551)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3711)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.os.MessageQueue.nativePollOnce(Native Method)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.os.MessageQueue.next(MessageQueue.java:138)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.os.Looper.loop(Looper.java:123)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ Caused by: java.io.IOException: No such file or directory
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.ProcessManager.exec(Native Method)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ at java.lang.ProcessManager.exec(ProcessManager.java:209)
10-11 08:37:13.985    2918-2918/com.smst.dotmav.smst W/System.err﹕ ... 48 more

我基本上想要运行任何不需要root的命令,我该如何解决?

提前致谢!

0 个答案:

没有答案