下面是我使用的方法,需要android:sharedUserId="android.uid.system"。
android设备是自己定制的设备,普通手机恐怕不行。
public static String installSliently(String filePath) throws IOException {String[] args = { "pm", "install", "-r", filePath };ProcessBuilder processBuilder = new ProcessBuilder(args);Process process = processBuilder.start();try{ByteArrayOutputStream baos = new ByteArrayOutputStream();int read = -1;baos.write("err:\n".getBytes());InputStream errIs = process.getErrorStream();try{while ((read = errIs.read()) != -1) {baos.write(read);}}finally{errIs.close();}baos.write('\n');baos.write("out:\n".getBytes());InputStream inIs = process.getInputStream();try{while ((read = inIs.read()) != -1) {baos.write(read);}}finally{inIs.close();}byte[] data = baos.toByteArray();return new String(data);}finally{process.destroy();}