I was researching and found that through the classes PackageInstaller, which is from api 21, it is possible to install third-party applications without user interaction. I am based on this example that is published in github:
https://github.com/googlesamples/android-testdpc
I have not been able to make it work, I also read that it is necessary that your application works as Device Owner of the device.
I found this code in more than one site, but I could not make it work, it does not generate error when sending the PendingIntent.
Here is the example code:
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;
}
private static IntentSender createIntentSender(Context context, int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new Intent(ACTION_INSTALL_COMPLETE),
0);
return pendingIntent.getIntentSender();
}
I try this with the app package that I want to install, but doesn't seems to work. If anyone can I will be very grateful.