javiersantos / AppUpdater

A library that checks for your apps' updates on Google Play, GitHub, Amazon, F-Droid or your own server. API 9+ required.
Apache License 2.0
1.98k stars 410 forks source link

Sir, Please guide me how to use DownloadManager class with your library? #193

Closed vabbydante closed 3 years ago

vabbydante commented 4 years ago

Sir, I am currently a college student and I am developing a project for my final year degree.. Sir your libraries work flawlessly! just one thing i wanted to ask, how do i make it to download the .apk file with DownloadManager? I mean, when i click on it a browser window opens. Also, if there is a way to download and open the PackageInstaller automatically I would be overwhelmed! Please sir, help me out? Thankyou sir!! :D Much love!!!

eduardoxcruz commented 3 years ago

Right now I just finished doing the function you are requesting. I'm in the same situation. I hope the code still works for you.

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

   //Verify various permissions. Specially for R/W data in internal storage
    verificacionDePermisos();

    //I make a method in another class to verify connection to internet. You can skip this validation
    if (conexionAInternet.VerificarConexion(getApplicationContext())) {

        appUpdater = new AppUpdater(MainActivity.this)
                .setDisplay(Display.DIALOG)
                .setUpdateFrom(UpdateFrom.XML)
                .setUpdateXML("YourServer/YourDocumentWithVersionInfo.xml")
                .setCancelable(false)
                .setTitleOnUpdateAvailable("A title")
                .setButtonUpdate("A name of button")
                .setButtonDismiss(null)
                .setButtonDoNotShowAgain(null)
                .setButtonUpdateClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        //Start the download update
                        descargarActualizacion();
                    }
                })
                .setIcon(R.drawable.alert); //I have a special icon. You can upload yours in the Drawable folder of your project or skip this parameter

        appUpdater.start();

    } else {
         //Another function of another class. It doesn't matter for AppUpdater
        alerta.Error(R.string.noExisteConexionAInternet, this, true);
    }
}

public void descargarActualizacion(){

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("YourServer/YourApp.apk"));
        request.setTitle("A title");
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "YourApp.apk" );
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        manager.enqueue(request);

        Toast.makeText(MainActivity.this, "Download started. Please wait...", Toast.LENGTH_LONG).show();
}

//Verify various permissions. Again, we want to have R/W permission to download the .apk in internal storage
public void verificacionDePermisos(){

    if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) +
            ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) +
            ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED)
    {
        if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){

            solicitudDePermisos();
        }
    }
}

 //We request the missing permissions
public void solicitudDePermisos(){

    //Using simple alertDialog to explain why we need the permissions
    AlertDialog.Builder alerta = new AlertDialog.Builder(MainActivity.this);
    alerta.setMessage(R.string.solicitarPermisos);
    alerta.setTitle(R.string.Aviso);
    alerta.setIcon(R.drawable.alert);
    alerta.setCancelable(false);
    alerta.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            ActivityCompat.requestPermissions(
                    MainActivity.this,
                    new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_CODE);

            //I wait 5 seconds to restart the activity to reload the permissions
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    recreate();
                }
            }, 5000);
        }
    });
    alerta.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //I force to user to agree the permissions for obvious rasons. If him do not accept the permissions, I close the app
            finish();
        }
    });

    AlertDialog mostrarDialog = alerta.create();
    mostrarDialog.show();
}

//You write your code here when the download finished
BroadcastReceiver onDownloadComplete = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(MainActivity.this, "Download finished.", Toast.LENGTH_LONG).show();

    }
};`