繁体   English   中英

安装新的APK文件以编程方式显示错误

[英]Installiation of new apk file programatically shows error

我在应用程序的oncreate中放置了条件检查,以检查版本更新。如果有新版本的应用程序,我将调用onDestroy。

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);
}

问题是执行我的安装方法时,它显示一个警告框

替换应用程序您正在安装的应用程序将替换另一个应用程序。 所有以前的用户数据将被保存。 并有确定和取消按钮

当我单击“确定”按钮时,它将显示另一个用于安装应用程序的按钮

但是,当我单击“安装”按钮时,应用程序将显示一个进度条,其中显示安装

然后,在那之后,我将获得一个未安装完成按钮的消息应用程序。

即我的新更新没有安装。

是我实施版本更新过程的正确方法。 请任何人进行审核。抱歉,冗长的代码。

这很可能是由于您首先从Eclipse将应用程序安装在设备上的事实。 这样做将使用一个证书对您的应用程序进行签名。

然后,将.apk文件放置在某个位置-要制作此.apk文件,必须使用证书对其进行签名。

Eclipse用来签署您的应用程序的证书与您用来签署.apk文件的证书不同-意味着当您下载.apk文件并尝试安装它时,会出现证书不匹配的情况,并且不会安装。

您可以做的是:

  1. 通过设备上的.apk文件安装应用
  2. 创建.apk文件的较新版本并将其放置在Web上。
  3. 在设备上运行应用程序,更新应该成功。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM