How to: download and install an application through Datalogic Android SDK

Starting from Android SDK version 1.25, and for most of the devices that integrate it, it is possible to install an apk using Datalogic API.
In this example, splitted into two parts, we show some code snippet of an application that downloads an apk from an HTTP Server and installs the apk on the device, at this link you can find the full documentation.
The application can be downloaded from here and appears as shown in the figure:

  1. Download the apk:
    The apk is downaloaded from an HTTP link to public “Download” directory ( default link is https://developer.datalogic.com/cgi-bin/tools/NewsFiles/ScanEngineTest.1.0.0.apk). A notification will be visible during the download and a “Toast” will be showed when the apk is completely downloaded.
    In this code snippet is reported the action triggered by the button “Download”.
private void download() {
        String url = inpuUrl.getEditableText().toString();
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle("Downloading apk");
        //clean directory
        File oldFile= new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)+"/"+fileDownloaded);
        if(oldFile.exists()){
            oldFile.delete();
        }
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        Uri Urifile = Uri.fromFile(new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)+"/"+fileDownloaded));
        request.setDestinationUri(Urifile);
        Log.d(TAG, "downloadUpdate: " + DIRECTORY_DOWNLOADS + fileDownloaded);
        DownloadManager downloadManager = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
        downloadManager.enqueue(request);

    }
  1. Install the apk:
    The button “Install” triggers the Datalogic API that will install the apk, a “Toast” is showed with the result of the operation.
    In this code snippet is reported the action triggred by the button “Install”
 private void install(){
       PackageInstaller packageInstaller = new PackageInstaller(getApplicationContext());
        PackageInstallerListener packageInstallerListener =  new PackageInstallerListener() {
            @Override
            public void onResult(List<PackageInstallerResult> list) {
                Toast.makeText(getApplicationContext(), PackageInstallerException.stringifyCode(list.get(0).result), Toast.LENGTH_LONG).show();
                Log.i(TAG, "install result" + PackageInstallerException.stringifyCode(list.get(0).result));
            }
        };


        PackageInstallerSession packageInstallerSession = packageInstaller.createSession(packageInstallerListener);
        int resultSession = packageInstallerSession.openSession();
        Log.i(TAG, "install openSession Result " + PackageInstallerException.stringifyCode(resultSession));


        File file = getApplicationContext().getExternalFilesDir(DIRECTORY_DOWNLOADS);
        packageInstallerSession.install(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)+"/"+fileDownloaded,true);
        resultSession=packageInstallerSession.closeSession();
        Log.i(TAG, "install closeSession Result " + PackageInstallerException.stringifyCode(resultSession));
    }
2 Likes