Plz check below classe & give me the suggestion for how to use them https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.an
This works for me as well, although my device owner restricts the installation of apps and unknown sources by the user. Even if I running this example as device admin, I've got the java.lang.SecurityException: User restriction prevents installing.
openSession is checking for permissions. With this simple modificaton it is possible to reset the user restrictions only during a short method call.
public static DevicePolicyManager getDpm(Context context) {
return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
}
public static ComponentName getAdmin(Context context) {
return new ComponentName(context, MyDevicePolicyReceiver.class);
}
public static void addMyRestrictions(Context context) {
getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS);
getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
}
public static void clearMyRestrictions(Context context) {
getDpm(context).clearUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS);
getDpm(context).clearUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
}
public static void installPackage(Context context, InputStream inputStream)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
//openSession checks for user restrictions
clearMyRestrictions(context);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
addMyRestrictions(context);
long sizeBytes = 0;
OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();
// fake intent
IntentSender statusReceiver = null;
Intent intent = new Intent(context, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
session.close();
}
Please take care of exception handling.