PackageInstaller
public
class
PackageInstaller
extends Object
java.lang.Object | |
↳ | android.content.pm.PackageInstaller |
Offers the ability to install, upgrade, and remove applications on the device. This includes support for apps packaged either as a single "monolithic" APK, or apps packaged as multiple "split" APKs.
An app is delivered for installation through a
PackageInstaller.Session
, which any app can create. Once the session
is created, the installer can stream one or more APKs into place until it
decides to either commit or destroy the session. Committing may require user
intervention to complete the installation, unless the caller falls into one of the
following categories, in which case the installation will complete automatically.
- the device owner
- the affiliated profile owner
Sessions can install brand new apps, upgrade existing apps, or add new splits into an existing app.
Apps packaged as multiple split APKs always consist of a single "base" APK
(with a null
split name) and zero or more "split" APKs (with unique
split names). Any subset of these APKs can be installed together, as long as
the following constraints are met:
- All APKs must have the exact same package name, version code, and signing certificates.
- All APKs must have unique split names.
- All installations must contain a single base APK.
The ApiDemos project contains examples of using this API:
ApiDemos/src/com/example/android/apis/content/InstallApk*.java
.
Summary
Nested classes | |
---|---|
class |
PackageInstaller.InstallConstraints
A class to encapsulate constraints for installation. |
class |
PackageInstaller.InstallConstraintsResult
The callback result of |
class |
PackageInstaller.PreapprovalDetails
Details for requesting the pre-commit install approval. |
class |
PackageInstaller.Session
An installation that is being actively staged. |
class |
PackageInstaller.SessionCallback
Events for observing session lifecycle. |
class |
PackageInstaller.SessionInfo
Details for an active install session. |
class |
PackageInstaller.SessionParams
Parameters for creating a new |
class |
PackageInstaller.UnarchivalState
Used to communicate the unarchival state in |
Constants | |
---|---|
String |
ACTION_SESSION_COMMITTED
Broadcast Action: Explicit broadcast sent to the last known default launcher when a session for a new install is committed. |
String |
ACTION_SESSION_DETAILS
Activity Action: Show details about a particular install session. |
String |
ACTION_SESSION_UPDATED
Broadcast Action: Send information about a staged install session when its state is updated. |
String |
EXTRA_INSTALL_CONSTRAINTS
The |
String |
EXTRA_INSTALL_CONSTRAINTS_RESULT
The |
String |
EXTRA_OTHER_PACKAGE_NAME
Another package name relevant to a status. |
String |
EXTRA_PACKAGE_NAME
Package name that an operation is working with. |
String |
EXTRA_PRE_APPROVAL
Indicate if the status is for a pre-approval request. |
String |
EXTRA_SESSION
|
String |
EXTRA_SESSION_ID
An integer session ID that an operation is working with. |
String |
EXTRA_STATUS
Current status of an operation. |
String |
EXTRA_STATUS_MESSAGE
Detailed string representation of the status, including raw details that are useful for debugging. |
String |
EXTRA_STORAGE_PATH
Storage path relevant to a status. |
String |
EXTRA_UNARCHIVE_ALL_USERS
If true, the requestor of the unarchival has specified that the app should be unarchived for all users. |
String |
EXTRA_UNARCHIVE_ID
Extra field for the unarchive ID. |
String |
EXTRA_UNARCHIVE_PACKAGE_NAME
Extra field for the package name of a package that is requested to be unarchived. |
String |
EXTRA_UNARCHIVE_STATUS
Current status of an unarchive operation. |
int |
PACKAGE_SOURCE_DOWNLOADED_FILE
Code indicating that the package being installed comes from a file that was downloaded to the device by the user. |
int |
PACKAGE_SOURCE_LOCAL_FILE
Code indicating that the package being installed comes from a local file on the device. |
int |
PACKAGE_SOURCE_OTHER
Code indicating that the package being installed is from a source not reflected by any other package source constant. |
int |
PACKAGE_SOURCE_STORE
Code indicating that the package being installed is from a store. |
int |
PACKAGE_SOURCE_UNSPECIFIED
The installer did not call |
int |
STATUS_FAILURE
The operation failed in a generic way. |
int |
STATUS_FAILURE_ABORTED
The operation failed because it was actively aborted. |
int |
STATUS_FAILURE_BLOCKED
The operation failed because it was blocked. |
int |
STATUS_FAILURE_CONFLICT
The operation failed because it conflicts (or is inconsistent with) with another package already installed on the device. |
int |
STATUS_FAILURE_INCOMPATIBLE
The operation failed because it is fundamentally incompatible with this device. |
int |
STATUS_FAILURE_INVALID
The operation failed because one or more of the APKs was invalid. |
int |
STATUS_FAILURE_STORAGE
The operation failed because of storage issues. |
int |
STATUS_FAILURE_TIMEOUT
The operation failed because it didn't complete within the specified timeout. |
int |
STATUS_PENDING_USER_ACTION
User action is currently required to proceed. |
int |
STATUS_SUCCESS
The operation succeeded. |
int |
UNARCHIVAL_ERROR_INSTALLER_DISABLED
The installer responsible for the unarchival is disabled. |
int |
UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED
The installer responsible for the unarchival has been uninstalled The system will return this status if appropriate. |
int |
UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE
Not enough storage to unarchive the application. |
int |
UNARCHIVAL_ERROR_NO_CONNECTIVITY
The device is not connected to the internet |
int |
UNARCHIVAL_ERROR_USER_ACTION_NEEDED
The user needs to interact with the installer to enable the installation. |
int |
UNARCHIVAL_GENERIC_ERROR
Generic error: The app cannot be unarchived. |
int |
UNARCHIVAL_OK
The unarchival is possible and will commence. |
Public methods | |
---|---|
void
|
abandonSession(int sessionId)
Completely abandon the given session, destroying all staged data and rendering it invalid. |
void
|
checkInstallConstraints(List<String> packageNames, PackageInstaller.InstallConstraints constraints, Executor executor, Consumer<PackageInstaller.InstallConstraintsResult> callback)
Check if install constraints are satisfied for the given packages. |
void
|
commitSessionAfterInstallConstraintsAreMet(int sessionId, IntentSender statusReceiver, PackageInstaller.InstallConstraints constraints, long timeoutMillis)
Commit the session when all constraints are satisfied. |
int
|
createSession(PackageInstaller.SessionParams params)
Create a new session using the given parameters, returning a unique ID that represents the session. |
PackageInstaller.SessionInfo
|
getActiveStagedSession()
This method was deprecated
in API level 30.
Use |
List<PackageInstaller.SessionInfo>
|
getActiveStagedSessions()
Returns list of active staged sessions. |
List<PackageInstaller.SessionInfo>
|
getAllSessions()
Return list of all known install sessions, regardless of the installer. |
List<PackageInstaller.SessionInfo>
|
getMySessions()
Return list of all known install sessions owned by the calling app. |
PackageInstaller.SessionInfo
|
getSessionInfo(int sessionId)
Return details for a specific session. |
List<PackageInstaller.SessionInfo>
|
getStagedSessions()
Return list of all staged install sessions. |
void
|
installExistingPackage(String packageName, int installReason, IntentSender statusReceiver)
Install the given package, which already exists on the device, for the user for which this installer was created. |
void
|
installPackageArchived(ArchivedPackageInfo archivedPackageInfo, PackageInstaller.SessionParams sessionParams, IntentSender statusReceiver)
Install package in an archived state. |
PackageInstaller.Session
|
openSession(int sessionId)
Open an existing session to actively perform work. |
void
|
registerSessionCallback(PackageInstaller.SessionCallback callback, Handler handler)
Register to watch for session lifecycle events. |
void
|
registerSessionCallback(PackageInstaller.SessionCallback callback)
Register to watch for session lifecycle events. |
void
|
reportUnarchivalState(PackageInstaller.UnarchivalState unarchivalState)
Reports the state of an unarchival to the system. |
void
|
reportUnarchivalStatus(int unarchiveId, int status, long requiredStorageBytes, PendingIntent userActionIntent)
Reports the status of an unarchival to the system. |
void
|
requestArchive(String packageName, IntentSender statusReceiver)
Requests to archive a package which is currently installed. |
void
|
requestUnarchive(String packageName, IntentSender statusReceiver)
Requests to unarchive a currently archived package. |
void
|
uninstall(String packageName, IntentSender statusReceiver)
Uninstall the given package, removing it completely from the device. |
void
|
uninstall(VersionedPackage versionedPackage, int flags, IntentSender statusReceiver)
Uninstall the given package with a specific version code, removing it completely from the device. |
void
|
uninstall(VersionedPackage versionedPackage, IntentSender statusReceiver)
Uninstall the given package with a specific version code, removing it completely from the device. |
void
|
uninstallExistingPackage(String packageName, IntentSender statusReceiver)
Uninstall the given package for the user for which this installer was created if the package will still exist for other users on the device. |
void
|
unregisterSessionCallback(PackageInstaller.SessionCallback callback)
Unregister a previously registered callback. |
void
|
updateSessionAppIcon(int sessionId, Bitmap appIcon)
Update the icon representing the app being installed in a specific session. |
void
|
updateSessionAppLabel(int sessionId, CharSequence appLabel)
Update the label representing the app being installed in a specific session. |
void
|
waitForInstallConstraints(List<String> packageNames, PackageInstaller.InstallConstraints constraints, IntentSender callback, long timeoutMillis)
Similar to |
Inherited methods | |
---|---|
Constants
ACTION_SESSION_COMMITTED
public static final String ACTION_SESSION_COMMITTED
Broadcast Action: Explicit broadcast sent to the last known default launcher when a session
for a new install is committed. For managed profile, this is sent to the default launcher
of the primary profile.
For user-profiles that have items restricted on home screen, this broadcast is sent to
the default launcher of the primary profile, only if it has either
ERROR(/Manifest.permission.ACCESS_HIDDEN_PROFILES_FULL)
or
ERROR(/Manifest.permission.ACCESS_HIDDEN_PROFILES)
permission.
The associated session is defined in EXTRA_SESSION
and the user for which this
session was created in Intent#EXTRA_USER
.
Constant Value: "android.content.pm.action.SESSION_COMMITTED"
ACTION_SESSION_DETAILS
public static final String ACTION_SESSION_DETAILS
Activity Action: Show details about a particular install session. This may surface actions such as pause, resume, or cancel.
This should always be scoped to the installer package that owns the
session. Clients should use SessionInfo#createDetailsIntent()
to
build this intent correctly.
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
The session to show details for is defined in EXTRA_SESSION_ID
.
Constant Value: "android.content.pm.action.SESSION_DETAILS"
ACTION_SESSION_UPDATED
public static final String ACTION_SESSION_UPDATED
Broadcast Action: Send information about a staged install session when its state is updated.
The associated session information is defined in EXTRA_SESSION
.
Constant Value: "android.content.pm.action.SESSION_UPDATED"
EXTRA_INSTALL_CONSTRAINTS
public static final String EXTRA_INSTALL_CONSTRAINTS
The InstallConstraints
object.
See also:
Constant Value: "android.content.pm.extra.INSTALL_CONSTRAINTS"
EXTRA_INSTALL_CONSTRAINTS_RESULT
public static final String EXTRA_INSTALL_CONSTRAINTS_RESULT
The InstallConstraintsResult
object.
See also:
Constant Value: "android.content.pm.extra.INSTALL_CONSTRAINTS_RESULT"
EXTRA_OTHER_PACKAGE_NAME
public static final String EXTRA_OTHER_PACKAGE_NAME
Another package name relevant to a status. This is typically the package responsible for causing an operation failure.
See also:
Constant Value: "android.content.pm.extra.OTHER_PACKAGE_NAME"
EXTRA_PACKAGE_NAME
public static final String EXTRA_PACKAGE_NAME
Package name that an operation is working with.
See also:
Constant Value: "android.content.pm.extra.PACKAGE_NAME"
EXTRA_PRE_APPROVAL
public static final String EXTRA_PRE_APPROVAL
Indicate if the status is for a pre-approval request.
If callers use the same IntentSender
for both
Session#requestUserPreapproval(PreapprovalDetails, IntentSender)
and
Session#commit(IntentSender)
, they can use this to differentiate between them.
See also:
Constant Value: "android.content.pm.extra.PRE_APPROVAL"
EXTRA_SESSION
public static final String EXTRA_SESSION
SessionInfo
that an operation is working with.
See also:
Constant Value: "android.content.pm.extra.SESSION"
EXTRA_SESSION_ID
public static final String EXTRA_SESSION_ID
An integer session ID that an operation is working with.
See also:
Constant Value: "android.content.pm.extra.SESSION_ID"
EXTRA_STATUS
public static final String EXTRA_STATUS
Current status of an operation. Will be one of
STATUS_PENDING_USER_ACTION
, STATUS_SUCCESS
,
STATUS_FAILURE
, STATUS_FAILURE_ABORTED
,
STATUS_FAILURE_BLOCKED
, STATUS_FAILURE_CONFLICT
,
STATUS_FAILURE_INCOMPATIBLE
, STATUS_FAILURE_INVALID
,
STATUS_FAILURE_STORAGE
, or STATUS_FAILURE_TIMEOUT
.
More information about a status may be available through additional extras; see the individual status documentation for details.
See also:
Constant Value: "android.content.pm.extra.STATUS"
EXTRA_STATUS_MESSAGE
public static final String EXTRA_STATUS_MESSAGE
Detailed string representation of the status, including raw details that are useful for debugging.
See also:
Constant Value: "android.content.pm.extra.STATUS_MESSAGE"
EXTRA_STORAGE_PATH
public static final String EXTRA_STORAGE_PATH
Storage path relevant to a status.
See also:
Constant Value: "android.content.pm.extra.STORAGE_PATH"
EXTRA_UNARCHIVE_ALL_USERS
public static final String EXTRA_UNARCHIVE_ALL_USERS
If true, the requestor of the unarchival has specified that the app should be unarchived
for all users. Sent as part of the Intent.ACTION_UNARCHIVE_PACKAGE
intent.
Constant Value: "android.content.pm.extra.UNARCHIVE_ALL_USERS"
EXTRA_UNARCHIVE_ID
public static final String EXTRA_UNARCHIVE_ID
Extra field for the unarchive ID. Sent as
part of the Intent.ACTION_UNARCHIVE_PACKAGE
intent.
Constant Value: "android.content.pm.extra.UNARCHIVE_ID"
EXTRA_UNARCHIVE_PACKAGE_NAME
public static final String EXTRA_UNARCHIVE_PACKAGE_NAME
Extra field for the package name of a package that is requested to be unarchived. Sent as
part of the Intent.ACTION_UNARCHIVE_PACKAGE
intent.
Constant Value: "android.content.pm.extra.UNARCHIVE_PACKAGE_NAME"
EXTRA_UNARCHIVE_STATUS
public static final String EXTRA_UNARCHIVE_STATUS
Current status of an unarchive operation. Will be one of
UNARCHIVAL_OK
, UNARCHIVAL_ERROR_USER_ACTION_NEEDED
,
UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE
, UNARCHIVAL_ERROR_NO_CONNECTIVITY
,
UNARCHIVAL_GENERIC_ERROR
, UNARCHIVAL_ERROR_INSTALLER_DISABLED
or
UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED
.
If the status is not UNARCHIVAL_OK
, then Intent#EXTRA_INTENT
will be set
with an intent for a corresponding follow-up action (e.g. storage clearing dialog) or a
failure dialog.
Used as part of requestUnarchive(String, IntentSender)
to return the status of the unarchival through
the IntentSender
.
See also:
Constant Value: "android.content.pm.extra.UNARCHIVE_STATUS"
PACKAGE_SOURCE_DOWNLOADED_FILE
public static final int PACKAGE_SOURCE_DOWNLOADED_FILE
Code indicating that the package being installed comes from a file that was downloaded to
the device by the user. For use in place of PACKAGE_SOURCE_LOCAL_FILE
when the
installer knows the package was downloaded.
Constant Value: 4 (0x00000004)
PACKAGE_SOURCE_LOCAL_FILE
public static final int PACKAGE_SOURCE_LOCAL_FILE
Code indicating that the package being installed comes from a local file on the device. A file manager that is facilitating the installation of an APK file would use this.
Constant Value: 3 (0x00000003)
PACKAGE_SOURCE_OTHER
public static final int PACKAGE_SOURCE_OTHER
Code indicating that the package being installed is from a source not reflected by any other package source constant.
Constant Value: 1 (0x00000001)
PACKAGE_SOURCE_STORE
public static final int PACKAGE_SOURCE_STORE
Code indicating that the package being installed is from a store. An app store that installs an app for the user would use this.
Constant Value: 2 (0x00000002)
PACKAGE_SOURCE_UNSPECIFIED
public static final int PACKAGE_SOURCE_UNSPECIFIED
The installer did not call PackageInstaller.SessionParams#setPackageSource(int)
to
specify the package source.
Constant Value: 0 (0x00000000)
STATUS_FAILURE
public static final int STATUS_FAILURE
The operation failed in a generic way. The system will always try to provide a more specific failure reason, but in some rare cases this may be delivered.
See also:
Constant Value: 1 (0x00000001)
STATUS_FAILURE_ABORTED
public static final int STATUS_FAILURE_ABORTED
The operation failed because it was actively aborted. For example, the user actively declined requested permissions, or the session was abandoned.
See also:
Constant Value: 3 (0x00000003)
STATUS_FAILURE_BLOCKED
public static final int STATUS_FAILURE_BLOCKED
The operation failed because it was blocked. For example, a device policy may be blocking the operation, a package verifier may have blocked the operation, or the app may be required for core system operation.
The result may also contain EXTRA_OTHER_PACKAGE_NAME
with the
specific package blocking the install.
Constant Value: 2 (0x00000002)
STATUS_FAILURE_CONFLICT
public static final int STATUS_FAILURE_CONFLICT
The operation failed because it conflicts (or is inconsistent with) with another package already installed on the device. For example, an existing permission, incompatible certificates, etc. The user may be able to uninstall another app to fix the issue.
The result may also contain EXTRA_OTHER_PACKAGE_NAME
with the
specific package identified as the cause of the conflict.
Constant Value: 5 (0x00000005)
STATUS_FAILURE_INCOMPATIBLE
public static final int STATUS_FAILURE_INCOMPATIBLE
The operation failed because it is fundamentally incompatible with this
device. For example, the app may require a hardware feature that doesn't
exist, it may be missing native code for the ABIs supported by the
device, or it requires a newer SDK version, etc.
Starting in Build.VERSION_CODES#UPSIDE_DOWN_CAKE
, an app with only 32-bit native
code can still be installed on a device that supports both 64-bit and 32-bit ABIs.
However, a warning dialog will be displayed when the app is launched.
See also:
Constant Value: 7 (0x00000007)
STATUS_FAILURE_INVALID
public static final int STATUS_FAILURE_INVALID
The operation failed because one or more of the APKs was invalid. For example, they might be malformed, corrupt, incorrectly signed, mismatched, etc.
See also:
Constant Value: 4 (0x00000004)
STATUS_FAILURE_STORAGE
public static final int STATUS_FAILURE_STORAGE
The operation failed because of storage issues. For example, the device may be running low on space, or external media may be unavailable. The user may be able to help free space or insert different external media.
The result may also contain EXTRA_STORAGE_PATH
with the path to
the storage device that caused the failure.
See also:
Constant Value: 6 (0x00000006)
STATUS_FAILURE_TIMEOUT
public static final int STATUS_FAILURE_TIMEOUT
The operation failed because it didn't complete within the specified timeout.
See also:
Constant Value: 8 (0x00000008)
STATUS_PENDING_USER_ACTION
public static final int STATUS_PENDING_USER_ACTION
User action is currently required to proceed. You can launch the intent
activity described by Intent#EXTRA_INTENT
to involve the user and
continue.
You may choose to immediately launch the intent if the user is actively using your app. Otherwise, you should use a notification to guide the user back into your app before launching.
See also:
Constant Value: -1 (0xffffffff)
STATUS_SUCCESS
public static final int STATUS_SUCCESS
The operation succeeded.
Constant Value: 0 (0x00000000)
UNARCHIVAL_ERROR_INSTALLER_DISABLED
public static final int UNARCHIVAL_ERROR_INSTALLER_DISABLED
The installer responsible for the unarchival is disabled.
The system will return this status if appropriate. Installers do not need to verify for this error.
Constant Value: 4 (0x00000004)
UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED
public static final int UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED
The installer responsible for the unarchival has been uninstalled
The system will return this status if appropriate. Installers do not need to verify for this error.
Constant Value: 5 (0x00000005)
UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE
public static final int UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE
Not enough storage to unarchive the application.
The installer can optionally provide a userActionIntent
for a space-clearing
dialog. If no action is provided, then a generic intent
StorageManager.ACTION_MANAGE_STORAGE
is started instead.
Constant Value: 2 (0x00000002)
UNARCHIVAL_ERROR_NO_CONNECTIVITY
public static final int UNARCHIVAL_ERROR_NO_CONNECTIVITY
The device is not connected to the internet
Constant Value: 3 (0x00000003)
UNARCHIVAL_ERROR_USER_ACTION_NEEDED
public static final int UNARCHIVAL_ERROR_USER_ACTION_NEEDED
The user needs to interact with the installer to enable the installation.
An example use case for this could be that the user needs to login to allow the download for a paid app.
Constant Value: 1 (0x00000001)
UNARCHIVAL_GENERIC_ERROR
public static final int UNARCHIVAL_GENERIC_ERROR
Generic error: The app cannot be unarchived.
Constant Value: 100 (0x00000064)
UNARCHIVAL_OK
public static final int UNARCHIVAL_OK
The unarchival is possible and will commence.
Note that this does not mean that the unarchival has completed. This status should be sent before any longer asynchronous action (e.g. app download) is started.
Constant Value: 0 (0x00000000)
Public methods
abandonSession
public void abandonSession (int sessionId)
Completely abandon the given session, destroying all staged data and
rendering it invalid. Abandoned sessions will be reported to
SessionCallback
listeners as failures. This is equivalent to
opening the session and calling Session#abandon()
.
Parameters | |
---|---|
sessionId |
int |
Throws | |
---|---|
SecurityException |
when the caller does not own the session, or the session is invalid. |
checkInstallConstraints
public void checkInstallConstraints (List<String> packageNames, PackageInstaller.InstallConstraints constraints, Executor executor, Consumer<PackageInstaller.InstallConstraintsResult> callback)
Check if install constraints are satisfied for the given packages. Note this query result is just a hint and subject to race because system states could change anytime in-between this query and committing the session. The result is returned by a callback because some constraints might take a long time to evaluate.
Parameters | |
---|---|
packageNames |
List : a list of package names to check the constraints for installation
This value cannot be null . |
constraints |
PackageInstaller.InstallConstraints : the constraints for installation.
This value cannot be null . |
executor |
Executor : the Executor on which to invoke the callback
This value cannot be null .
Callback and listener events are dispatched through this
Executor , providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor() .
Otherwise, provide an Executor that dispatches to an appropriate thread. |
callback |
Consumer : called when the InstallConstraintsResult is ready
This value cannot be null . |
Throws | |
---|---|
SecurityException |
if the given packages' installer of record doesn't match the caller's own package name or the installerPackageName set by the caller doesn't match the caller's own package name. |
commitSessionAfterInstallConstraintsAreMet
public void commitSessionAfterInstallConstraintsAreMet (int sessionId, IntentSender statusReceiver, PackageInstaller.InstallConstraints constraints, long timeoutMillis)
Commit the session when all constraints are satisfied. This is a convenient method to
combine waitForInstallConstraints(java.util.List, android.content.pm.PackageInstaller.InstallConstraints, android.content.IntentSender, long)
and Session#commit(IntentSender)
.
Once this method is called, the session is sealed and no additional mutations
may be performed on the session. In the case of timeout, you may commit the
session again using this method or Session#commit(IntentSender)
for retries.
Parameters | |
---|---|
sessionId |
int : the session ID to commit when all constraints are satisfied. |
statusReceiver |
IntentSender : Called when the state of the session changes. Intents
sent to this receiver contain EXTRA_STATUS .
Refer to the individual status codes on how to handle them.
This value cannot be null . |
constraints |
PackageInstaller.InstallConstraints : The requirements to satisfy before committing the session.
This value cannot be null . |
timeoutMillis |
long : The maximum time to wait, in milliseconds until the
constraints are satisfied. The caller will be notified via
statusReceiver if timeout happens before commit.
Value is a non-negative duration in milliseconds. |
Throws | |
---|---|
IllegalArgumentException |
if the statusReceiver from an immutable
PendingIntent when caller has a target SDK of API
35 or above. |
createSession
public int createSession (PackageInstaller.SessionParams params)
Create a new session using the given parameters, returning a unique ID that represents the session. Once created, the session can be opened multiple times across multiple device boots.
The system may automatically destroy sessions that have not been finalized (either committed or abandoned) within a reasonable period of time, typically on the order of a day.
Parameters | |
---|---|
params |
PackageInstaller.SessionParams : This value cannot be null . |
Returns | |
---|---|
int |
positive, non-zero unique ID that represents the created session. This ID remains consistent across device reboots until the session is finalized. IDs are not reused during a given boot. |
Throws | |
---|---|
IOException |
if parameters were unsatisfiable, such as lack of disk space or unavailable media. |
SecurityException |
when installation services are unavailable, such as when called from a restricted user. |
IllegalArgumentException |
when SessionParams is invalid. |
getActiveStagedSession
public PackageInstaller.SessionInfo getActiveStagedSession ()
This method was deprecated
in API level 30.
Use getActiveStagedSessions()
as there can be more than one active staged
session
Returns first active staged session, or null
if there is none.
For more information on what sessions are considered active see
SessionInfo#isStagedSessionActive()
.
Returns | |
---|---|
PackageInstaller.SessionInfo |
getActiveStagedSessions
public List<PackageInstaller.SessionInfo> getActiveStagedSessions ()
Returns list of active staged sessions. Returns empty list if there is none.
For more information on what sessions are considered active see
* SessionInfo#isStagedSessionActive()
.
Returns | |
---|---|
List<PackageInstaller.SessionInfo> |
This value cannot be null . |
getAllSessions
public List<PackageInstaller.SessionInfo> getAllSessions ()
Return list of all known install sessions, regardless of the installer. Callers need to either declare <queries> element with the specific package name in the app's manifest, have the android.permission.QUERY_ALL_PACKAGES, or be the session owner to retrieve these details.
Returns | |
---|---|
List<PackageInstaller.SessionInfo> |
This value cannot be null . |
getMySessions
public List<PackageInstaller.SessionInfo> getMySessions ()
Return list of all known install sessions owned by the calling app.
Returns | |
---|---|
List<PackageInstaller.SessionInfo> |
This value cannot be null . |
getSessionInfo
public PackageInstaller.SessionInfo getSessionInfo (int sessionId)
Return details for a specific session. Callers need to either declare <queries> element with the specific package name in the app's manifest, have the android.permission.QUERY_ALL_PACKAGES, or be the session owner to retrieve these details.
Parameters | |
---|---|
sessionId |
int |
Returns | |
---|---|
PackageInstaller.SessionInfo |
details for the requested session, or null if the session
does not exist. |
getStagedSessions
public List<PackageInstaller.SessionInfo> getStagedSessions ()
Return list of all staged install sessions. Callers need to either declare <queries> element with the specific package name in the app's manifest, have the android.permission.QUERY_ALL_PACKAGES, or be the session owner to retrieve these details.
Returns | |
---|---|
List<PackageInstaller.SessionInfo> |
This value cannot be null . |
installExistingPackage
public void installExistingPackage (String packageName, int installReason, IntentSender statusReceiver)
Install the given package, which already exists on the device, for the user for which this installer was created.
This will
all restricted permissions
.
Requires Manifest.permission.INSTALL_PACKAGES
and android.Manifest.permission.INSTALL_EXISTING_PACKAGES
Parameters | |
---|---|
packageName |
String : The package to install.
This value cannot be null . |
installReason |
int : Reason for install.
Value is PackageManager.INSTALL_REASON_UNKNOWN , PackageManager.INSTALL_REASON_POLICY , PackageManager.INSTALL_REASON_DEVICE_RESTORE , PackageManager.INSTALL_REASON_DEVICE_SETUP , PackageManager.INSTALL_REASON_USER , or android.content.pm.PackageManager.INSTALL_REASON_ROLLBACK |
statusReceiver |
IntentSender : Where to deliver the result of the operation indicated by the extra
EXTRA_STATUS . Refer to the individual status codes
on how to handle them.
This value may be null . |
installPackageArchived
public void installPackageArchived (ArchivedPackageInfo archivedPackageInfo, PackageInstaller.SessionParams sessionParams, IntentSender statusReceiver)
Install package in an archived state.
Requires Manifest.permission.INSTALL_PACKAGES
Parameters | |
---|---|
archivedPackageInfo |
ArchivedPackageInfo : archived package data such as package name, signature etc.
This value cannot be null . |
sessionParams |
PackageInstaller.SessionParams : used to create an underlying installation session
This value cannot be null . |
statusReceiver |
IntentSender : Called when the state of the session changes. Intents
sent to this receiver contain EXTRA_STATUS . Refer to the
individual status codes on how to handle them.
This value cannot be null . |
openSession
public PackageInstaller.Session openSession (int sessionId)
Open an existing session to actively perform work. To succeed, the caller must be the owner of the install session.
Parameters | |
---|---|
sessionId |
int |
Returns | |
---|---|
PackageInstaller.Session |
This value cannot be null . |
Throws | |
---|---|
IOException |
if parameters were unsatisfiable, such as lack of disk space or unavailable media. |
SecurityException |
when the caller does not own the session, or the session is invalid. |
registerSessionCallback
public void registerSessionCallback (PackageInstaller.SessionCallback callback, Handler handler)
Register to watch for session lifecycle events. No special permissions are required to watch for these events.
Parameters | |
---|---|
callback |
PackageInstaller.SessionCallback : This value cannot be null . |
handler |
Handler : to dispatch callback events through, otherwise uses
calling thread.
This value cannot be null . |
registerSessionCallback
public void registerSessionCallback (PackageInstaller.SessionCallback callback)
Register to watch for session lifecycle events. The callers need to be the session owner or have the android.permission.QUERY_ALL_PACKAGES to watch for these events.
Parameters | |
---|---|
callback |
PackageInstaller.SessionCallback : This value cannot be null . |
reportUnarchivalState
public void reportUnarchivalState (PackageInstaller.UnarchivalState unarchivalState)
Reports the state of an unarchival to the system.
Requires Manifest.permission.INSTALL_PACKAGES
or Manifest.permission.REQUEST_INSTALL_PACKAGES
Parameters | |
---|---|
unarchivalState |
PackageInstaller.UnarchivalState : This value cannot be null . |
Throws | |
---|---|
PackageManager.NameNotFoundException |
if no unarchival with unarchiveId exists |
See also:
reportUnarchivalStatus
public void reportUnarchivalStatus (int unarchiveId, int status, long requiredStorageBytes, PendingIntent userActionIntent)
Reports the status of an unarchival to the system.
Requires Manifest.permission.INSTALL_PACKAGES
or Manifest.permission.REQUEST_INSTALL_PACKAGES
Parameters | |
---|---|
unarchiveId |
int : the ID provided by the system as part of the
intent.action.UNARCHIVE broadcast with EXTRA_UNARCHIVE_ID. |
status |
int : is used for the system to provide the user with necessary
follow-up steps or errors.
Value is android.content.pm.PackageInstaller.UNARCHIVAL_STATUS_UNSET, UNARCHIVAL_OK , UNARCHIVAL_ERROR_USER_ACTION_NEEDED , UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE , UNARCHIVAL_ERROR_NO_CONNECTIVITY , UNARCHIVAL_ERROR_INSTALLER_DISABLED , UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED , or UNARCHIVAL_GENERIC_ERROR |
requiredStorageBytes |
long : If the error is UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE this field
should be set to specify how many additional bytes of storage
are required to unarchive the app. |
userActionIntent |
PendingIntent : Optional intent to start a follow up action required to
facilitate the unarchival flow (e.g. user needs to log in).
This value may be null . |
Throws | |
---|---|
PackageManager.NameNotFoundException |
if no unarchival with unarchiveId exists |
requestArchive
public void requestArchive (String packageName, IntentSender statusReceiver)
Requests to archive a package which is currently installed.
During the archival process, the apps APKs and cache are removed from the device while
the user data is kept. Through the requestUnarchive(String, IntentSender)
call, apps
can be restored again through their responsible installer.
Archived apps are returned as displayable apps through the LauncherApps
APIs and
will be displayed to users with UI treatment to highlight that said apps are archived. If
a user taps on an archived app, the app will be unarchived and the restoration process is
communicated.
Requires Manifest.permission.DELETE_PACKAGES
or Manifest.permission.REQUEST_DELETE_PACKAGES
Parameters | |
---|---|
packageName |
String : This value cannot be null . |
statusReceiver |
IntentSender : Callback used to notify when the operation is completed.
This value cannot be null . |
Throws | |
---|---|
PackageManager.NameNotFoundException |
If packageName isn't found or not
available to the caller or isn't archived. |
requestUnarchive
public void requestUnarchive (String packageName, IntentSender statusReceiver)
Requests to unarchive a currently archived package.
Sends a request to unarchive an app to the responsible installer. The installer is
determined by InstallSourceInfo#getUpdateOwnerPackageName()
, or
InstallSourceInfo#getInstallingPackageName()
if the former value is null.
The installation will happen asynchronously and can be observed through
Intent.ACTION_PACKAGE_ADDED
.
Requires Manifest.permission.INSTALL_PACKAGES
or Manifest.permission.REQUEST_INSTALL_PACKAGES
Parameters | |
---|---|
packageName |
String : This value cannot be null . |
statusReceiver |
IntentSender : Callback used to notify whether the installer has accepted the
unarchival request or an error has occurred. The status update will be
sent though EXTRA_UNARCHIVE_STATUS . Only one status will be
sent.
This value cannot be null . |
Throws | |
---|---|
PackageManager.NameNotFoundException |
If packageName isn't found or not
visible to the caller or if the package has no
installer on the device anymore to unarchive it. |
IOException |
If parameters were unsatisfiable, such as lack of disk space. |
uninstall
public void uninstall (String packageName, IntentSender statusReceiver)
Uninstall the given package, removing it completely from the device. This method is available to:
- the current "installer of record" for the package
- the device owner
- the affiliated profile owner
Requires
Manifest.permission.DELETE_PACKAGES
or Manifest.permission.REQUEST_DELETE_PACKAGES
Parameters | |
---|---|
packageName |
String : The package to uninstall.
This value cannot be null . |
statusReceiver |
IntentSender : Where to deliver the result of the operation indicated by the extra
EXTRA_STATUS . Refer to the individual status codes
on how to handle them.
This value cannot be null . |
See also:
uninstall
public void uninstall (VersionedPackage versionedPackage, int flags, IntentSender statusReceiver)
Uninstall the given package with a specific version code, removing it
completely from the device. This method is only available to the current
"installer of record" for the package. If the version code of the package
does not match the one passed in the versioned package argument this
method is a no-op. Use PackageManager#VERSION_CODE_HIGHEST
to
uninstall the latest version of the package.
Requires Manifest.permission.DELETE_PACKAGES
or Manifest.permission.REQUEST_DELETE_PACKAGES
Parameters | |
---|---|
versionedPackage |
VersionedPackage : The versioned package to uninstall.
This value cannot be null . |
flags |
int : Flags for uninstall.
Value is either 0 or a combination of android.content.pm.PackageManager.DELETE_KEEP_DATA, android.content.pm.PackageManager.DELETE_ALL_USERS, android.content.pm.PackageManager.DELETE_SYSTEM_APP, android.content.pm.PackageManager.DELETE_DONT_KILL_APP, and android.content.pm.PackageManager.DELETE_CHATTY |
statusReceiver |
IntentSender : Where to deliver the result of the operation indicated by the extra
EXTRA_STATUS . Refer to the individual status codes
on how to handle them.
This value cannot be null . |
uninstall
public void uninstall (VersionedPackage versionedPackage, IntentSender statusReceiver)
Uninstall the given package with a specific version code, removing it
completely from the device. If the version code of the package
does not match the one passed in the versioned package argument this
method is a no-op. Use PackageManager#VERSION_CODE_HIGHEST
to
uninstall the latest version of the package.
This method is available to:
- the current "installer of record" for the package
- the device owner
- the affiliated profile owner
Requires
Manifest.permission.DELETE_PACKAGES
or Manifest.permission.REQUEST_DELETE_PACKAGES
Parameters | |
---|---|
versionedPackage |
VersionedPackage : The versioned package to uninstall.
This value cannot be null . |
statusReceiver |
IntentSender : Where to deliver the result of the operation indicated by the extra
EXTRA_STATUS . Refer to the individual status codes
on how to handle them.
This value cannot be null . |
See also:
uninstallExistingPackage
public void uninstallExistingPackage (String packageName, IntentSender statusReceiver)
Uninstall the given package for the user for which this installer was created if the package
will still exist for other users on the device.
Requires Manifest.permission.DELETE_PACKAGES
Parameters | |
---|---|
packageName |
String : The package to uninstall.
This value cannot be null . |
statusReceiver |
IntentSender : Where to deliver the result of the operation indicated by the extra
EXTRA_STATUS . Refer to the individual status codes
on how to handle them.
This value may be null . |
unregisterSessionCallback
public void unregisterSessionCallback (PackageInstaller.SessionCallback callback)
Unregister a previously registered callback.
Parameters | |
---|---|
callback |
PackageInstaller.SessionCallback : This value cannot be null . |
updateSessionAppIcon
public void updateSessionAppIcon (int sessionId, Bitmap appIcon)
Update the icon representing the app being installed in a specific
session. This should be roughly
ActivityManager#getLauncherLargeIconSize()
in both dimensions.
Parameters | |
---|---|
sessionId |
int |
appIcon |
Bitmap : This value may be null . |
Throws | |
---|---|
SecurityException |
when the caller does not own the session, or the session is invalid. |
updateSessionAppLabel
public void updateSessionAppLabel (int sessionId, CharSequence appLabel)
Update the label representing the app being installed in a specific session.
Parameters | |
---|---|
sessionId |
int |
appLabel |
CharSequence : This value may be null . |
Throws | |
---|---|
SecurityException |
when the caller does not own the session, or the session is invalid. |
waitForInstallConstraints
public void waitForInstallConstraints (List<String> packageNames, PackageInstaller.InstallConstraints constraints, IntentSender callback, long timeoutMillis)
Similar to checkInstallConstraints(java.util.List, android.content.pm.PackageInstaller.InstallConstraints, java.util.concurrent.Executor, java.util.function.Consumer)
,
but the callback is invoked only when the constraints are satisfied or after timeout.
Note: the device idle constraint might take a long time to evaluate. The system will ensure the constraint is evaluated completely before handling timeout.
Parameters | |
---|---|
packageNames |
List : a list of package names to check the constraints for installation
This value cannot be null . |
constraints |
PackageInstaller.InstallConstraints : the constraints for installation.
This value cannot be null . |
callback |
IntentSender : Called when the constraints are satisfied or after timeout.
Intents sent to this callback contain:
Intent#EXTRA_PACKAGES for the input package names,
EXTRA_INSTALL_CONSTRAINTS for the input constraints,
EXTRA_INSTALL_CONSTRAINTS_RESULT for the result.
This value cannot be null . |
timeoutMillis |
long : The maximum time to wait, in milliseconds until the constraints are
satisfied. Valid range is from 0 to one week. 0 means the
callback will be invoked immediately no matter constraints are
satisfied or not.
Value is a non-negative duration in milliseconds. |
Throws | |
---|---|
SecurityException |
if the given packages' installer of record doesn't match the caller's own package name or the installerPackageName set by the caller doesn't match the caller's own package name. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-06-18 UTC.