2012-07-19 184 views
2

我试图在Android Terminal Emulator上运行命令。我有我的设备根植,我安装了superuser.apk运行命令Android终端模拟器。安装按代码安装

我做这样的:

try { 
     Process process = Runtime.getRuntime().exec(cmd); 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line = bufferedReader.readLine(); 
     while (line != null) { 
      Log.i("SHELL", line); 
      line = bufferedReader.readLine(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

而且也很好:

try { 
     Process process = Runtime.getRuntime().exec(<b>cmd</b>*); 
     process.waitFor(); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

我试图安装一个。 Apk通过代码。我尝试了以下方法:

cmd =>pm install /mnt/sdcard/app.apk 没有任何结果。

在cmd =>su -c "pm install /mnt/sdcard/app.apk" 随着单引号,双quoutes和无引号。结果:

I/SHELL(1432): Usage: su [options] [LOGIN] 

    I/SHELL(1432): Options: 
    I/SHELL(1432): -c, --command COMMAND   pass COMMAND to the invoked shell 
    I/SHELL(1432): -h, --help     display this help message and exit 
    I/SHELL(1432): -, -l, --login    make the shell a login shell 
    I/SHELL(1432): -s, --shell SHELL    use SHELL instead of the default in passwd 
    I/SHELL(1432): -v, --version     display version number and exit 
    I/SHELL(1432): -V       display version code and exit. this is 
    I/SHELL(1432):         used almost exclusively by Superuser.apk 

其他命令状LS运行正常。

如何安装位置在/ mnt/SD卡的APK?

谢谢!

回答

4

试试这个:

su -c "pm install /mnt/sdcard/app.apk" root 

但我认为,如果你有没有根您的手机不工作

+0

它不起作用。我找到了解决方案。见下文。 – Ricmcm 2012-07-20 07:42:11

0

我有一个疑问,当你执行一个命令,它不是一个ADB壳牌它的应用程序的外壳环境。

从码:(如果你想通过意向做,然后)

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(new File(path+"/<application_name>.apk")), "application/vnd.android.package-archive"); 
startActivity(intent); 

这是权限..

<uses-permission android:name="android.permission.INSTALL_PACKAGES" /> 
+0

我需要远程将它安装在多个设备上。我需要静静地做。我找到了解决方案。 – Ricmcm 2012-07-20 07:40:45

+0

请在这里发布您的解决方案。谢谢 – 2013-05-15 08:07:20

3

我找到了解决办法。

Process p; 
    try { 
      // Preform su to get root privledges 
      p = Runtime.getRuntime().exec("su"); 

      // Attempt to write a file to a root-only 
      DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
      os.writeBytes(**any command**+"\n"); 

      // Close the terminal 
      os.writeBytes("exit\n"); 
      os.flush(); 
      try { 
        p.waitFor(); 
        if (p.exitValue() != 255) { 
          // Sucess :-) 
        } 
        else { 
          // Fail 
        } 

      } catch (InterruptedException e) { 
        // Fail 
      } 
    } catch (IOException e) { 
      // Fail 
    } 

谢谢大家!

+0

也从这个命令你能够从你的应用程序安装apk文件? – user370305 2012-07-20 08:03:06

+0

是的。 - > os.writeBytes(“pm install app.apk \ n”); – Ricmcm 2012-07-20 16:13:25

+0

@Ricmcm:我试过你的代码。但是我在这行“os.writeBytes(”pm install myapp.apk \ n“)”中出现“写入失败:EPIPE(Broken pipe)”错误。可能是什么问题。你能帮我解决这个问题吗? – Manoj 2012-11-06 05:52:05