1. Are you ready for the Galaxy S20? Here is everything we know so far!

PackageInstaller not prompting

Discussion in 'Android Development' started by Arni Richard Arnason, Jul 31, 2020.

  1. Arni Richard Arnason

    Thread Starter

    I am using PackageInstaller to prompt for an update of own app from apk file.

    The commit() call is not prompting for the update, and there is no exception.
    Does anyone have an idea?

    CODE:

    void promptApkUpdate(Activity activity, File file)
    {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
    PackageInstaller.Session session = null;

    try
    {
    PackageInstaller packageInstaller = activity.getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
    PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(activity.getPackageName());
    int sessionId = packageInstaller.createSession(params);
    session = packageInstaller.openSession(sessionId);
    packageInstaller.registerSessionCallback(createCallBack());
    addApkToInstallSession(file, session);
    // Create an install status receiver.
    Intent intent = new Intent(activity, MainActivity.class);
    intent.setAction(PACKAGE_INSTALLED_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
    activity,
    sessionId,
    new Intent(PACKAGE_INSTALLED_ACTION),
    0);
    IntentSender statusReceiver = pendingIntent.getIntentSender();
    // Commit the session (this will start the installation workflow).
    session.commit(statusReceiver);
    }
    catch (IOException e)
    {
    throw new RuntimeException("Couldn't install package", e);
    }
    catch (RuntimeException e)
    {
    if (session != null)
    session.abandon();
    }
    }
    }

    void addApkToInstallSession(File apkFile, PackageInstaller.Session session)
    throws IOException
    {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
    // It's recommended to pass the file size to openWrite(). Otherwise installation may fail
    // if the disk is almost full.
    try
    (
    OutputStream packageInSession = session.openWrite("package", 0, apkFile.length());
    InputStream is = new FileInputStream(apkFile.getAbsoluteFile());
    )
    {
    byte[] buffer = new byte[16384];
    int n;
    while ((n = is.read(buffer)) >= 0)
    {
    packageInSession.write(buffer, 0, n);
    }
    }
    }
    }
     


Loading...

Share This Page

Loading...