2011-11-17 100 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. 在设备上运行应用程序,更新应该成功。