2011-11-17 64 views
-1

我已經在檢查版本更新的應用程序的oncreate中放置了條件檢查。如果我的應用程序的新版本可用,我將調用onDestroy。安裝新的apk文件編程方式顯示錯誤

public void onCreate(Bundle savedInstanceState) { 
    if(「true」.equal(CheckVersion)) 
    { 
     alertbox.setMessage("Do you want to update Aplication with Latest version?"); 
     alertbox.setPositiveButton("Yes", 
       new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 

       try { 

        onDestroy(); 
       } catch (Exception exception) { 

        exception.toString(); 
       }      

      } 
     }); 
     alertbox.setNegativeButton("No", 
       new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       LaunchManifest(); 
      } 
     }); 
     alertbox.show(); 
    } 
} 

/* 
* In the onDestroy method I have Placed the code for downloading the New 
* apk file and installation of the apk file methods as given below 
*/ 
@Override 
public void onDestroy() { 
    DownloadOnSDcard(); 
    InstallApplication(); 
} 

public void DownloadOnSDcard() { 
    try { 

     urlpath = "http://192.168.1.158/VisionEPODWebService/VisionEPOD.apk"; 
     String ApkName = "VisionEPOD.apk"; 

     URL url = new URL(urlpath.toString()); 
     // Your given URL. 
     HttpURLConnection c = (HttpURLConnection)url.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 
     // Connection Complete here.! 
     // Toast.makeText(getApplicationContext(), 
     // "HttpURLConnection complete.", Toast.LENGTH_SHORT).show(); 
     String PATH = Environment.getExternalStorageDirectory() + "/download/"; 
     File file = new File(PATH); // PATH = /mnt/sdcard/download/ 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 
     File outputFile = new File(file, ApkName.toString()); 
     FileOutputStream fos = new FileOutputStream(outputFile); 
     // Toast.makeText(getApplicationContext(), "SD Card Path: " + 
     // outputFile.toString(), Toast.LENGTH_SHORT).show(); 
     InputStream is = c.getInputStream(); 
     // Get from Server and Catch In Input Stream Object. 
     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     while ((len1 = is.read(buffer)) != -1) { 
      fos.write(buffer, 0, len1); // Write In FileOutputStream. 
     } 
     fos.close(); 
     is.close(); 
     // till here, it works fine - .apk is download to my sdcard in 
     // download file. 
     // So plz Check in DDMS tab and Select your Emualtor. 
     // Toast.makeText(getApplicationContext(), 
     // "Download Complete on SD Card.!", Toast.LENGTH_SHORT).show(); 
     // download the APK to sdcard then fire the Intent. 
    } catch (IOException e) { 
     Toast.makeText(getApplicationContext(), "Error! " + e.toString(), Toast.LENGTH_LONG) 
     .show(); 
    } 
} 

public void InstallApplication() { 
    String ApkName = "VisionEPOD.apk"; 
    String PackageName = "com.Vision.EPOD"; 
    Uri packageURI = Uri.parse(PackageName.toString()); 
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, packageURI); 
    intent.setDataAndType(
      Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" 
        + ApkName.toString())), "application/vnd.android.package-archive"); 

    startActivity(intent); 
} 

的問題是,當得到執行我的安裝方法它顯示一個警告框,

替換應用 您正在安裝會替換其他應用程序中的應用。所有以前的用戶數據將被保存。 並有確定和取消按鈕

當我點擊確定按鈕,它顯示安裝的應用程序

的另一個按鈕,但是當我點擊安裝按鈕,應用程序顯示出,然後進行安裝

一個進度條我將得到一個未安裝完成按鈕的消息應用程序。

即我的新更新沒有安裝。

這是我實施版本更新程序的正確方法。請問任何人請查看。請留下冗長的代碼。

+0

請看看logcat的輸出。那裏也應該有一個錯誤信息。如果您發佈此消息,我們可能會幫助您。 – Janusz

回答

2

這很可能是由於您首先將應用程序從Eclipse安裝到設備上。這樣做將使用一個證書在您的應用程序上簽名。

然後,您將.apk文件放置在某個位置 - 要創建此.apk文件,必須使用證書對其進行簽名。

Eclipse對您的應用程序簽名的證書與您在.apk文件上簽名的證書不同,它意味着當您下載.apk文件並嘗試安裝它時,會出現證書不匹配,它不會安裝。

你可以做的是:

  1. 安裝通過.apk文件,文件的應用設備
  2. 上創建的.apk文件的更新版本,並將其放置在網絡上。
  3. 在設備上運行應用程序,更新應該成功。