I am trying to install a apk file using Package Installer. My app is current profile owner. The method is completing successfully but in my BroadcastReceiver I am getting PackageInstaller.STATUS_PENDING_USER_ACTION. As per android docs https://developer.android.com/reference/android/content/pm/PackageInstaller app should be able to silently install apk if it is a profile owner.
public static boolean installPackage(Context context, InputStream
in,
String packageName)
throws IOException {
final PackageInstaller packageInstaller =
context.getPackageManager().getPackageInstaller();
final PackageInstaller.SessionParams params = new
PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
// set params
final int sessionId = packageInstaller.createSession(params);
final PackageInstaller.Session session =
packageInstaller.openSession(sessionId);
final OutputStream out = session.openWrite("TestDPC", 0, -1);
final 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(createInstallIntentSender(context, sessionId));
return true;
}
Your app must be set as device owner or affiliated profile owner.