Installiation of new apk file programatically shows error
<p>I have placed a conditional checking in the oncreate of my application for checking the version update.If New version of my application available I will call the onDestroy.        </p>

<pre><code>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);
}
</code></pre>

<p>The problem is that when my method for installation is got executed it shows an alert box </p>

<p>Replace application
The application you are installing will replace another application. All previous user data will be saved.
And have OK and Cancel button</p>

<p>When I clicked ok button it show another button for installation of the application</p>

<p>But when I clicked the Installation button the application shows a progress bar showing installing</p>

<p>Then after that I will got a message application not installed with done button.</p>

<p>ie My new updates not got installed.</p>

<p>Is it the right way I have implemented the version update procedure. Will any one review please.Sorry for the lengthy code.</p>
