install / uninstall APKs programmatically (PackageManager vs Intents)

后端 未结 10 2022
时光取名叫无心
时光取名叫无心 2020-11-22 13:53

My application installs other applications, and it needs to keep track of what applications it has installed. Of course, this could be achieved by simply keeping a list of i

相关标签:
10条回答
  • 2020-11-22 14:27

    Prerequisite:

    Your APK needs to be signed by system as correctly pointed out earlier. One way to achieve that is building the AOSP image yourself and adding the source code into the build.

    Code:

    Once installed as a system app, you can use the package manager methods to install and uninstall an APK as following:

    Install:

    public boolean install(final String apkPath, final Context context) {
        Log.d(TAG, "Installing apk at " + apkPath);
        try {
            final Uri apkUri = Uri.fromFile(new File(apkPath));
            final String installerPackageName = "MyInstaller";
            context.getPackageManager().installPackage(apkUri, installObserver, PackageManager.INSTALL_REPLACE_EXISTING, installerPackageName);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    

    Uninstall:

    public boolean uninstall(final String packageName, final Context context) {
        Log.d(TAG, "Uninstalling package " + packageName);
        try {
            context.getPackageManager().deletePackage(packageName, deleteObserver, PackageManager.DELETE_ALL_USERS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    

    To have a callback once your APK is installed/uninstalled you can use this:

    /**
     * Callback after a package was installed be it success or failure.
     */
    private class InstallObserver implements IPackageInstallObserver {
    
        @Override
        public void packageInstalled(String packageName, int returnCode) throws RemoteException {
    
            if (packageName != null) {
                Log.d(TAG, "Successfully installed package " + packageName);
                callback.onAppInstalled(true, packageName);
            } else {
                Log.e(TAG, "Failed to install package.");
                callback.onAppInstalled(false, null);
            }
        }
    
        @Override
        public IBinder asBinder() {
            return null;
        }
    }
    
    /**
     * Callback after a package was deleted be it success or failure.
     */
    private class DeleteObserver implements IPackageDeleteObserver {
    
        @Override
        public void packageDeleted(String packageName, int returnCode) throws RemoteException {
            if (packageName != null) {
                Log.d(TAG, "Successfully uninstalled package " + packageName);
                callback.onAppUninstalled(true, packageName);
            } else {
                Log.e(TAG, "Failed to uninstall package.");
                callback.onAppUninstalled(false, null);
            }
        }
    
        @Override
        public IBinder asBinder() {
            return null;
        }
    }
    
    /**
     * Callback to give the flow back to the calling class.
     */
    public interface InstallerCallback {
        void onAppInstalled(final boolean success, final String packageName);
        void onAppUninstalled(final boolean success, final String packageName);
    }
    
    0 讨论(0)
  • 2020-11-22 14:28

    If you are passing package name as parameter to any of your user defined function then use the below code :

        Intent intent=new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:"+packageName));
        startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 14:33

    On a rooted device, you might use:

    String pkg = context.getPackageName();
    String shellCmd = "rm -r /data/app/" + pkg + "*.apk\n"
                    + "rm -r /data/data/" + pkg + "\n"
                    // TODO remove data on the sd card
                    + "sync\n"
                    + "reboot\n";
    Util.sudo(shellCmd);
    

    Util.sudo() is defined here.

    0 讨论(0)
  • 2020-11-22 14:35

    If you have Device Owner (or profile owner, I haven't tried) permission you can silently install/uninstall packages using device owner API.

    for uninstalling:

    public boolean uninstallPackage(Context context, String packageName) {
        ComponentName name = new ComponentName(MyAppName, MyDeviceAdminReceiver.class.getCanonicalName());
        PackageManager packageManger = context.getPackageManager();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            PackageInstaller packageInstaller = packageManger.getPackageInstaller();
            PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                    PackageInstaller.SessionParams.MODE_FULL_INSTALL);
            params.setAppPackageName(packageName);
            int sessionId = 0;
            try {
                sessionId = packageInstaller.createSession(params);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            packageInstaller.uninstall(packageName, PendingIntent.getBroadcast(context, sessionId,
                    new Intent("android.intent.action.MAIN"), 0).getIntentSender());
            return true;
        }
        System.err.println("old sdk");
        return false;
    }
    

    and to install package:

    public boolean installPackage(Context context,
                                         String packageName, String packagePath) {
        ComponentName name = new ComponentName(MyAppName, MyDeviceAdminReceiver.class.getCanonicalName());
        PackageManager packageManger = context.getPackageManager();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            PackageInstaller packageInstaller = packageManger.getPackageInstaller();
            PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                    PackageInstaller.SessionParams.MODE_FULL_INSTALL);
            params.setAppPackageName(packageName);
            try {
                int sessionId = packageInstaller.createSession(params);
                PackageInstaller.Session session = packageInstaller.openSession(sessionId);
                OutputStream out = session.openWrite(packageName + ".apk", 0, -1);
                readTo(packagePath, out); //read the apk content and write it to out
                session.fsync(out);
                out.close();
                System.out.println("installing...");
                session.commit(PendingIntent.getBroadcast(context, sessionId,
                        new Intent("android.intent.action.MAIN"), 0).getIntentSender());
                System.out.println("install request sent");
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        System.err.println("old sdk");
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题