public class

PackageInstaller

extends Object
java.lang.Object
   ↳ com.datalogic.device.app.PackageInstaller

Class Overview

PackageInstaller gives developers the ability to install, uninstall and upgrade applications in unattended/silent mode. The install/uninstall/upgrade/selfUpgrade methods are asynchronous. An object that implements PackageInstallerListener must be passed to PackageInstaller to be able to handle the result.
An install/uninstall/upgrade method can be executed alone calling the corresponding method of the PackageInstaller or together with other methods grouped in a PackageInstallerSession. In the former case there will be a result callback for each method, in the latter case there will be only one callback listing the results of each method. The methods grouped in a PackageInstallerSession are executed in best effort.

To install an application 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 command. This object must be passed as parameter to the install method.
  3. To perform the install session call install(String, boolean, PackageInstallerListener). The method onResult of the listener will be called when the install session has completely finished, either with success or failure.

To uninstall an application 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 command. This object must be passed as parameter to the uninstall method.
  3. To perform the uninstall session call uninstall(String, PackageInstallerListener). The method onResult of the listener will be called when the uninstall session has completely finished, either with success or failure.

To upgrade an application 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 command. This object must be passed as parameter to the upgrade method.
  3. To perform the upgrade session call upgrade(String, PackageInstallerListener). The method onResult of the listener will be called when the upgrade session has completely finished, either with success or failure.

To execute more install/uninstall/upgrade methods and be notified of the results when all the methods have completed, 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. This object must be passed as parameter to the createSession method.
  3. Obtain an instance of PackageInstallerSession with createSession(PackageInstallerListener).
  4. Open a transaction on the session calling openSession().
  5. Call the methods install(String, boolean), uninstall(String), upgrade(String) that suite you.
  6. Close and commit the transaction on the session calling closeSession().
  7. The method onResult of the listener will be called when all the methods listed in the session have completed, either with success or failure.

To upgrade the current application do the following steps:

  1. Obtain an instance of PackageInstaller with PackageInstaller(Context).
  2. Call selfUpgrade(String) passing as parameter the name of the package to be used to upgrade the current application.

Summary

Public Constructors
PackageInstaller(Context context)
This is the constructor of PackageInstaller.
Public Methods
PackageInstallerSession createSession(PackageInstallerListener listener)
Call this method to create a PackageInstallerSession object.
int install(String apk, boolean force, PackageInstallerListener listener)
Call this method to install the application contained within the .apk file specified by apk.
int selfUpgrade(String apk)
Call this method to upgrade the current application.
int uninstall(String packageName, PackageInstallerListener listener)
Call this method to unistall an application installed on the device.
int upgrade(String apk, PackageInstallerListener listener)
Call this method to upgrade an application installed on the device.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public PackageInstaller (Context context)

Added in revision 25

This is the constructor of PackageInstaller.

Parameters
context Context context of the caller application
Throws
PackageInstallerException in case of error.

Public Methods

public PackageInstallerSession createSession (PackageInstallerListener listener)

Added in revision 25

Call this method to create a PackageInstallerSession object. The method can be used to request a session on which it will be possible to group the execution of a sequence of install/uninstall/upgrade commands.

Parameters
listener PackageInstallerListener instance that implements the method onResult(List) to handle the result of methods executed in the session.
Returns
Throws
PackageInstallerException in case of error when exceptions are enabled through the ErrorManager singleton.

public int install (String apk, boolean force, PackageInstallerListener listener)

Added in revision 25

Call this method to install the application contained within the .apk file specified by apk.
The method is executed alone, not grouped together with other methods as can be done using a PackageInstallerSession. The result of the command is received through the listener method onResult(List). Do not suspend the UIThread while waiting for the result of the method otherwise an application’s deadlock can occur.

Example:

PackageInstaller packageInstaller = new PackageInstaller(context);
PackageInstallerListener listener = new PackageInstallerListener{public void onResult(List results) {...}};
packageInstaller.install("/dir1/dir11/dir116/pkg1.apk", false, listener);

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.
listener PackageInstallerListener instance that implements the method onResult(List) to handle the result of the method.
Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.

public int selfUpgrade (String apk)

Added in revision 25

Call this method to upgrade the current application.
Do not suspend the UIThread while waiting for the result of the method otherwise an application’s deadlock can occur. In case of success the intent "android.intent.action.MY_PACKAGE_REPLACED" is fired.
In case of failure:
- if the problem is found before contacting the remote service an error is returned; the error code is one of the PackageInstallerException error constants, but when exceptions are enabled @throws PackageInstallerException.
- if the problem is found in the remote service the intent "com.datalogic.app.UPGRADE_ME_STATUS" is fired.
The extra data of the "com.datalogic.app.UPGRADE_ME_STATUS" are:
- android.content.pm.PackageInstaller.EXTRA_STATUS int: a possible error code, matching one of the PackageInstallerException error constants.
- android.content.pm.PackageInstaller.EXTRA_STATUS_MESSAGE String: an error string message.
- android.content.pm.PackageInstaller.EXTRA_PACKAGE_NAME String: Package Name of the application.

Parameters
apk String full path of the .apk to be used to upgrade the current application.
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.

public int uninstall (String packageName, PackageInstallerListener listener)

Added in revision 25

Call this method to unistall an application installed on the device.
The method is executed alone, not grouped together with other methods as can be done using a PackageInstallerSession. The result of the command is received through the listener method onResult(List). Do not suspend the UIThread while waiting for the result of the method otherwise an application’s deadlock can occur.

Example:

PackageInstaller packageInstaller = new PackageInstaller(context);
PackageInstallerListener listener = new PackageInstallerListener{public void onResult(List results) {...}};
packageInstaller.uninstall("com.example.helloandroid", listener);

Parameters
packageName String package name of the application to be uninstalled.
listener PackageInstallerListener instance that implements the method onResult(List) to handle the result of the method.
Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.

public int upgrade (String apk, PackageInstallerListener listener)

Added in revision 25

Call this method to upgrade an application installed on the device.
The method is executed alone, not grouped together with other methods as we can be done using a PackageInstallerSession. 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) {...}};
packageInstaller.upgrade("/dir1/dir11/dir116/pkg1_v2.apk", listener);

Parameters
apk String full path of the .apk to be used to upgrade the application already installed that has 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.
listener PackageInstallerListener instance that implements the method onResult(List) to handle the result of the method.
Returns
Throws
PackageInstallerException in case of error, when exceptions are enabled through the ErrorManager singleton.