public class

PackageInstallerSession

extends Object
implements ServiceConnection
java.lang.Object
   ↳ com.datalogic.device.app.PackageInstallerSession

Class Overview

PackageInstallerSession gives developers the ability to group the execution of a sequence of install, uninstall and upgrade commands. Once the session is created, the commands install, uninstall, upgrade must be called inside a transaction. A transaction is opened calling openSession() and closed and commited calling closeSession().

To install two applications using a session do the following steps:

  1. Obtain an instance of PackageInstaller with PackageInstaller(Context).
  2. Obtain an instance of PackageInstallerListener that will be used to handle the result of the commands invoked on the session.
  3. Create a PackageInstallerSession calling createSession(PackageInstallerListener). The createSession(PackageInstallerListener) method takes as parameters the instance of PackageInstallerListener created at the previous step.
  4. Start a session calling openSession().
  5. Call install(String, boolean) to install the first application.
  6. Call install(String, boolean) to install the second application.
  7. Close and commit the session calling closeSession().
  8. The method onResult of the listener will be called when the execution of all the commands of the transaction are completed, either with success or failure. It contains the result of each command.

Summary

Public Constructors
PackageInstallerSession()
Public Methods
int closeSession()
Call this method to execute the commands queued in the session.
int install(String apk, boolean force)
Call this method to install the application contained within the .apk file specified by apk.
int openSession()
Call this method to start a session.
int uninstall(String packageName)
Call this method to unistall an application installed on the device.
int upgrade(String apk)
Call this method to upgrade an application installed on the device.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.content.ServiceConnection

Public Constructors

public PackageInstallerSession ()

Added in revision 25

Public Methods

public int closeSession ()

Added in revision 25

Call this method to execute the commands queued in the session.
The commands will be executed in best effort.
The command execution is asynchronous. The results of each command will be delivered in a unique ordered list through the listener onResult(List) method. Do not suspend the UIThread while waiting for the result of the method otherwise an application’s deadlock can occur.

public int install (String apk, boolean force)

Added in revision 25

Call this method to install the application contained within the .apk file specified by apk.
The method must be called after having called the openSession().
The method is queued internally and executed only when the closeSession() is called.
The result of the command is received through the listener method onResult(List).

Example:

PackageInstaller packageInstaller = new PackageInstaller(context);
PackageInstallerListener listener = new PackageInstallerListener{public void onResult(List results) {...}};
PackageInstallerSession session = packageInstaller.createSession(listener);
session.openSession();
session.install("/dir1/dir11/dir116/pkg1.apk", force);
session.closeSession();

Parameters
apk String full path of the .apk to be installed.
NOTE: Running on an Android 11 device, until Datalogic SDK v1.32, an user App can install an APK only using
a path of the external storage (WRITE_EXTERNAL_STORAGE permission to be granted by App to store the APK)
or any path not requiring memory permissions to access (e.g Downloads).
On the contrary, if the calling App stores the APK into the internal memory,
the API will be not able to install the APK due to the missing privilages for accessing to the internal memory of the calling App.
From Datalogic SDK v1.33, instead, the API is able to install an APK stored into the internal memory of the calling App.
force boolean true force the upgrade in case the application is already installed, otherwise the upgrade is refused.
Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.

public int openSession ()

Added in revision 25

Call this method to start a session.
After this method, call the commands install, uninstall, upgrade to be executed.
The commands will be queued and executed only when the closeSession() method is called.
The commands will be executed in best effort.
The results of each command will be delivered in a unique ordered list through the onResult(List) method.

Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.

public int uninstall (String packageName)

Added in revision 25

Call this method to unistall an application installed on the device. The method must be called after having called the openSession().
The method is queued internally and executed when the closeSession() is called.
The result of the command is received through the listener method onResult(List).

Example:

PackageInstaller packageInstaller = new PackageInstaller(context);
PackageInstallerListener listener = new PackageInstallerListener{public void onResult(List results) {...}};
PackageInstallerSession session = packageInstaller.createSession(listener);
session.openSession();
session.uninstall("com.example.helloandroid");
session.closeSession();

Parameters
packageName String package name of the application to be uninstalled.
Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.

public int upgrade (String apk)

Added in revision 25

Call this method to upgrade an application installed on the device.
The method must be called after having called the openSession().
The method is queued internally and executed when the closeSession() is called.
The result of the command is received through the listener method onResult(List).

Example:

PackageInstaller packageInstaller = new PackageInstaller(context);
PackageInstallerListener listener = new PackageInstallerListener{public void onResult(List results) {...}};
PackageInstallerSession session = packageInstaller.createSession(listener);
session.openSession();
session.upgrade("/dir1/dir11/dir116/pkg1_v2.apk");
session.closeSession();

Parameters
apk String full path of the .apk to be used to upgrade the application already installed with the same Package Name of this .apk.
NOTE: Running on an Android 11 device, until Datalogic SDK v1.32, an user App can upgrade an APK only using
a path of the external storage (WRITE_EXTERNAL_STORAGE permission to be granted by App to store the APK)
or any path not requiring memory permissions to access (e.g Downloads).
On the contrary, if the calling App stores the APK into the internal memory,
the API will be not able to upgrade the APK due to the missing privilages for accessing to the internal memory of the calling App.
From Datalogic SDK v1.33, instead, the API is able to upgrade an APK stored into the internal memory of the calling App.
Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.