Android APIs
public static class

PackageInstaller.Session

extends Object
implements Closeable
java.lang.Object
   ↳ android.content.pm.PackageInstaller.Session

Class Overview

An installation that is being actively staged. For an install to succeed, all existing and new packages must have identical package names, version codes, and signing certificates.

A session may contain any number of split packages. If the application does not yet exist, this session must include a base package.

If an APK included in this session is already defined by the existing installation (for example, the same split name), the APK in this session will replace the existing APK.

Summary

Public Methods
void abandon()
Completely abandon this session, destroying all staged data and rendering it invalid.
void close()
Release this session object.
void commit(IntentSender statusReceiver)
Attempt to commit everything staged in this session.
void fsync(OutputStream out)
Ensure that any outstanding data for given stream has been committed to disk.
String[] getNames()
Return all APK names contained in this session.
InputStream openRead(String name)
Open a stream to read an APK file from the session.
OutputStream openWrite(String name, long offsetBytes, long lengthBytes)
Open a stream to write an APK file into the session.
void setStagingProgress(float progress)
Set current progress of staging this session.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public Methods

public void abandon ()

Added in API level 21

Completely abandon this session, destroying all staged data and rendering it invalid. Abandoned sessions will be reported to PackageInstaller.SessionCallback listeners as failures. This is equivalent to opening the session and calling abandon().

public void close ()

Added in API level 21

Release this session object. You can open the session again if it hasn't been finalized.

public void commit (IntentSender statusReceiver)

Added in API level 21

Attempt to commit everything staged in this session. This may require user intervention, and so it may not happen immediately. The final result of the commit will be reported through the given callback.

Once this method is called, no additional mutations may be performed on the session. If the device reboots before the session has been finalized, you may commit the session again.

Throws
SecurityException if streams opened through openWrite(String, long, long) are still open.

public void fsync (OutputStream out)

Added in API level 21

Ensure that any outstanding data for given stream has been committed to disk. This is only valid for streams returned from openWrite(String, long, long).

Throws
IOException

public String[] getNames ()

Added in API level 21

Return all APK names contained in this session.

This returns all names which have been previously written through openWrite(String, long, long) as part of this session.

Throws
SecurityException if called after the session has been committed or abandoned.
IOException

public InputStream openRead (String name)

Added in API level 21

Open a stream to read an APK file from the session.

This is only valid for names which have been previously written through openWrite(String, long, long) as part of this session. For example, this stream may be used to calculate a MessageDigest of a written APK before committing.

Throws
SecurityException if called after the session has been committed or abandoned.
IOException

public OutputStream openWrite (String name, long offsetBytes, long lengthBytes)

Added in API level 21

Open a stream to write an APK file into the session.

The returned stream will start writing data at the requested offset in the underlying file, which can be used to resume a partially written file. If a valid file length is specified, the system will preallocate the underlying disk space to optimize placement on disk. It's strongly recommended to provide a valid file length when known.

You can write data into the returned stream, optionally call fsync(OutputStream) as needed to ensure bytes have been persisted to disk, and then close when finished. All streams must be closed before calling commit(IntentSender).

Parameters
name arbitrary, unique name of your choosing to identify the APK being written. You can open a file again for additional writes (such as after a reboot) by using the same name. This name is only meaningful within the context of a single install session.
offsetBytes offset into the file to begin writing at, or 0 to start at the beginning of the file.
lengthBytes total size of the file being written, used to preallocate the underlying disk space, or -1 if unknown. The system may clear various caches as needed to allocate this space.
Throws
IOException if trouble opening the file for writing, such as lack of disk space or unavailable media.
SecurityException if called after the session has been committed or abandoned.

public void setStagingProgress (float progress)

Added in API level 21

Set current progress of staging this session. Valid values are anywhere between 0 and 1.

Note that this progress may not directly correspond to the value reported by onProgressChanged(int, float), as the system may carve out a portion of the overall progress to represent its own internal installation work.