install Package Silent - Android App

Android examples for App:Package

Description

install Package Silent

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {

    private static boolean installSilent(String packageUrl) {
        String[] args = { "pm", "install", "-r", packageUrl };
        String result = "";
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;/*  ww w  .  j a v  a2 s  .co m*/
        InputStream errIs = null;
        InputStream inIs = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            inIs = process.getInputStream();
            read = -1;
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (inIs != null)
                    inIs.close();
                if (errIs != null)
                    errIs.close();
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (process != null)
                process.destroy();
        }
        return result.contains("uccess") ? true : false;
    }
}

Related Tutorials