androidupdates

install update.apk only if you downloaded


How can I make the following code to activate only if there update.apk downloaded file? If that is not downloaded, do not try installing it. In current mode installs and activates instant error occurs if there update.apk.

public class Categori extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categori);

        // Buscar AdView como recurso y cargar una solicitud.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        WebView wv = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = wv.getSettings();
        webSettings.setBuiltInZoomControls(true);
        wv.loadUrl("http://android.vrt.ro/tv-update/ap5.html");

        Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(Uri.parse("file:///sdcard/updates/update.apk"),
                        "application/vnd.android.package-archive");
        startActivity(promptInstall);



        inapoibuton();
        filmebuton();
        nationalebuton();
        stiributon();
        muzica();
        documentarebunton();
        sportbuton();
        altlebuton();
        pentrupiticibuton();

        xxxbuton();

    }

Solution

  • You can check if the apk file exists like this:

        String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
        String path = sdcard.concat("/updates/update.apk");
        if (new File(path).exists()) {
            Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                    .setDataAndType(
                            Uri.parse("file:///sdcard/updates/update.apk"),
                            "application/vnd.android.package-archive"
                    );
            startActivity(promptInstall);
        }