Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install/update/remove APK using "PackageInstaller" class in Android L?

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?

like image 394
Sud Avatar asked Nov 12 '14 10:11

Sud


People also ask

How do I open APK with package installer?

If your phone's web browser doesn't give you the option to open the file after downloading, open your file explorer app, go to the Downloads folder on your device, then tap the APK file. Allow the app any required permissions it asks for. Then, at the bottom of the installer window, tap Install.

How do I fix an app not installed on Android?

By default Android allows installation only from the Play Store. In order to allow installation of apps from other sources, open the Settings app and locate "Install Unknown Apps" under Privacy/Security settings. Enable the permission for the app which you use to install your APK.

What is package installer Update?

Offers the ability to install, upgrade, and remove applications on the device. This includes support for apps packaged either as a single "monolithic" APK, or apps packaged as multiple "split" APKs. An app is delivered for installation through a PackageInstaller. Session , which any app can create.


2 Answers

It is possible without System permissions from Android M onwards.

if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid)         == PackageManager.PERMISSION_GRANTED)         || (installerUid == Process.ROOT_UID)         || mIsInstallerDeviceOwner) {     mPermissionsAccepted = true; } else {     mPermissionsAccepted = false; } 

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.

  • Make your app the Device owner.

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; }    private static IntentSender createIntentSender(Context context, int sessionId) {         PendingIntent pendingIntent = PendingIntent.getBroadcast(                 context,                 sessionId,                 new Intent(ACTION_INSTALL_COMPLETE),                 0);         return pendingIntent.getIntentSender();     } 

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()); 

Git repo here.

like image 141
amalBit Avatar answered Oct 14 '22 13:10

amalBit


You cannot silently install a third party application in the newly created user with PackageInstaller.Session.commit() without specific "rights".
You either need :

  • the INSTALL_PACKAGES permission. But this permission is not available for third-party application. So even with your profile owner app, you won't have this specific permission.
  • Run the process as ROOT_UID. Which means you'll have to root the device.

From the Android source code:

if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid) == PackageManager.PERMISSION_GRANTED)     || (installerUid == Process.ROOT_UID)) {     mPermissionsAccepted = true; } else {     mPermissionsAccepted = false; } 

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 commit process of the PackageInstaller's session. Obviously, in this case, this is not transparent, since the user will have to manually confirm the installation of your apps.

like image 22
Florent Dupont Avatar answered Oct 14 '22 14:10

Florent Dupont