2012-07-19 183 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