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

前端 未结 7 2180
长发绾君心
长发绾君心 2020-11-30 23:22

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

7条回答
  •  抹茶落季
    2020-11-30 23:36

    In Android Api-21 below is code snippet via which we can install apk silently.

    private void runInstallWrite() throws IOException, RemoteException {
            long sizeBytes = -1;
    
            String opt;
            while ((opt = nextOption()) != null) {
                if (opt.equals("-S")) {
                    sizeBytes = Long.parseLong(nextOptionData());
                } else {
                    throw new IllegalArgumentException("Unknown option: " + opt);
                }
            }
    
            final int sessionId = Integer.parseInt(nextArg());
            final String splitName = nextArg();
    
            String path = nextArg();
            if ("-".equals(path)) {
                path = null;
            } else if (path != null) {
                final File file = new File(path);
                if (file.isFile()) {
                    sizeBytes = file.length();
                }
            }
    
            final SessionInfo info = mInstaller.getSessionInfo(sessionId);
    
            PackageInstaller.Session session = null;
            InputStream in = null;
            OutputStream out = null;
            try {
                session = new PackageInstaller.Session(mInstaller.openSession(sessionId));
    
                if (path != null) {
                    in = new FileInputStream(path);
                } else {
                    in = new SizedInputStream(System.in, sizeBytes);
                }
                out = session.openWrite(splitName, 0, sizeBytes);
    
                int total = 0;
                byte[] buffer = new byte[65536];
                int c;
                while ((c = in.read(buffer)) != -1) {
                    total += c;
                    out.write(buffer, 0, c);
    
                    if (info.sizeBytes > 0) {
                        final float fraction = ((float) c / (float) info.sizeBytes);
                        session.addProgress(fraction);
                    }
                }
                session.fsync(out);
    
                System.out.println("Success: streamed " + total + " bytes");
            } finally {
                IoUtils.closeQuietly(out);
                IoUtils.closeQuietly(in);
                IoUtils.closeQuietly(session);
            }
        }
    

    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.

提交回复
热议问题