I'm trying to install packages on my device (from a device-owner app that runs on it) only by using the PackageInstaller and PackageManager APIs. I have been looking for examples but couldn't find anything that fit my need.
Here is a sample of my code to install the Facebook app:
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 guess, the next function I need to launch is a "commit(IntentSender statusReceiver)".
So Please tell me how to use commit() and especially how to declare a proper IntentSender to install the APK which is stored in /sdcard.
Thanks !!
This is possible from Android 6.0 and up.
Once your app gets the Device owner permission, we can install, uninstall and update silently without any user intervention.
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;
}
Uninstall:
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());
So after a few days looking for a way to use a IntentSender, I figured out that it is actually contained in a PendingIntent.
Here is a code that can be used:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With