【问题标题】:execute adb shell command at runtime from the android application在运行时从 android 应用程序执行 adb shell 命令
【发布时间】:2011-02-06 23:19:49
【问题描述】:

在我的应用程序中,我想在运行时从我的应用程序在 sdcard 中创建一个目录 xyz。

但它不起作用。

这是我的代码..

public class process extends Activity
{

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String[] str ={"mkdir","/sdcard/xyz"};

    try { 
        Process ps = Runtime.getRuntime().exec(str);
        try {
            ps.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
    } catch (IOException e) {
        Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
  }

}
}

【问题讨论】:

  • 您不能从应用程序运行 adb shell 命令,因为 adb shell 是通过 adb 守护程序获得的 shell。目前,您可以作为应用程序用户运行通用 shell 命令,但官方不鼓励这样做。考虑这样做的理由相对较少,在您的申请过程中可以轻松完成的事情绝对不是其中之一。

标签: android shell command execute adb


【解决方案1】:

使用以下代码在 sdcard 中制作目录。

File dir = new File("/mnt/sdcard/xyz");
try{
    if(dir.mkDir()) {
        System.out.println("Directory created");
    } else {
        System.out.println("Directory is not created");
}catch(Exception e){
  e.printStacktrace();
}

并将下面的使用权限添加到 android 清单文件中。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【讨论】:

    【解决方案2】:

    我不知道你是否可以在 Android 中执行 exec() 脚本,我强烈怀疑你不能。

    无论如何,您都不需要创建目录。这样做:

    new File("/sdcard/xyz").mkdirs();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2014-03-12
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多