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.android.com/reference/android/content/pm/PackageInstaller.Session.html
So please give me an example to install/update/remove app. Can it be possible that the new application will install in device profile owner?
It is possible without System permissions from Android M onwards.
Silent install and uninstall of apps by Device Owner:
A Device Owner can now silently install and uninstall applications using the PackageInstaller APIs, independent of Google Play for Work.
More in this link.
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.
Uninstall:
Git repo here.
In Android Api-21 below is code snippet via which we can install apk silently.
The above code is been took from Framework here
Can i use this code with device_owner or normal user in LoLiipop ?
Answer - No Since there are apis which is been @hide tag in android frameworks, although PackageManager.Session is introduced in API 21 but we cannot use new PAckageManager.Session() since it @hide in API 21.
If you wanna still use this code via framework.jar , you need to build Lolippop source code and extract jar from out/..../framework.jar and call above apis.
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.Please take care of exception handling.
The install method @amalBit provided did not work for me. It's strange since this is how it is implemented in the Google Sample.
This answer helped me to find a solution. I had to change some parts of the code. Here is my implementation:
This method can be called like this:
INSTALL:
UNINSTALL:
You cannot silently install a third party application in the newly created user with PackageInstaller.Session.commit() without specific "rights".
You either need :
ROOT_UID
. Which means you'll have to root the device.From the Android source code:
If you neither have root access and the
INSTALL_PACKAGES
permission, then a message will be prompted to the user to ask if he confirms the permissions. This confirmation is then used during the commitprocess
of thePackageInstaller's
session. Obviously, in this case, this is not transparent, since the user will have to manually confirm the installation of your apps.