2016-11-18 121 views
0

我試圖在沒有任何提示的情況下默默安裝apk。java.io.IOException:運行exec()時出錯。命令:Working Directory:null環境:null

這是使用adb命令安裝apk文件的代碼。

public void InstallAPK(String filename){ 
    File file = new File(filename); 
    if(file.exists()){ 
     try { 
      String command; 
      command = "adb install -r " + filename; 
      Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command }); 
      proc.waitFor(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

但是當我運行這段代碼時,出現以下錯誤。

java.io.IOException:運行exec()時發生錯誤。命令:[蘇,-c,亞行安裝-r /storage/emulated/0/Download/sampleapp.apk]工作指南:空環境:空

我已經給這些權限。

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

請有人可以幫我解決這個錯誤。

+0

你能找到解決方案嗎?如果是這樣,請讓我知道。 – Jaydev

回答

0

您不能在應用程序範圍內靜默運行命令以在該應用程序範圍之外安裝apk。

但是您可以提出讓AOSP做到這一點的意圖。檢查這個答案也許:Install Application programmatically on Android

Intent promptInstall = new Intent(Intent.ACTION_VIEW) 
    .setDataAndType(Uri.parse("file:///path/to/your.apk"), 
        "application/vnd.android.package-archive"); 
startActivity(promptInstall); 
+2

謝謝。我已經知道了。但我試圖在沒有提示的情況下安裝。 – Sridhar

+0

不適用於任何電話。你可以根據你自己的手機做到這一點,但你不想要。 – cokceken