Android Lollipop - PackageInstaller.Session commit ()

I try to install packages on my device (from the application that owns the device that runs on it), only using the PackageInstaller and PackageManager APIs. I searched for examples, but could not find anything that matched my need.

Here is an example of my code for installing the Facebook application:

PackageManager pm = getPackageManager();
PackageInstaller mPackageInstaller = pm.getPackageInstaller();
PackageInstaller.SessionParams mSessionParams = new PackageInstaller.SessionParams(SessionParams.MODE_FULL_INSTALL);

mSessionParams.setReferrerUri(Uri.parse("file:///mnt/sdcard/Download/Facebook.apk"));
int mSessionId = mPackageInstaller.createSession( mSessionParams );

PackageInstaller.Session mPkgSession = PackageInstaller.openSession(mSessionId);
OutputStream mOStream = mPkgSession.openWrite("com.facebook.katana", 0, -1);
mPkgSession.fsync(mOStream);

I think the next function I need to run is "commit (IntentSender statusReceiver)".

So, tell me how to use commit () and especially how to declare the correct IntentSender to install the APK, which is stored in / sdcard.

Thanks!

+4
3

PackageInstaller.Session.commit() "". :

INSTALL_PACKAGES. . , . ROOT_UID. , .

0

Android 6.0 .

  • .

, , - .

public static boolean installPackage(Context context, InputStream in, String packageName)
        throws IOException {
    PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);
    // set params
    int sessionId = packageInstaller.createSession(params);
    PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    OutputStream out = session.openWrite("COSU", 0, -1);
    byte[] buffer = new byte[65536];
    int c;
    while ((c = in.read(buffer)) != -1) {
        out.write(buffer, 0, c);
    }
    session.fsync(out);
    in.close();
    out.close();

    session.commit(createIntentSender(context, sessionId));
    return true;
}

String appPackage = "com.your.app.package";
Intent intent = new Intent(getActivity(), getActivity().getClass());
PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
PackageInstaller mPackageInstaller = getActivity().getPackageManager().getPackageInstaller();
mPackageInstaller.uninstall(appPackage, sender.getIntentSender());
+3

So, after a few days, to find a way to use IntentSender, I realized that it really is contained in PendingIntent.

Here is the code you can use:

Intent coucou = new Intent(this, ReceivedCommitActivity.class);
coucou.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pCoucou = PendingIntent.getActivity(this, 0, coucou,0);
IntentSender mIntentSender = pCoucou.getIntentSender();

mPkgSession.commit(mIntentSender);
+1
source

Source: https://habr.com/ru/post/1607643/


All Articles