1. // apk静默安装  
  2.     private class ApkUtil  
  3.         extends Thread {  
  4.         private boolean mEnable = true;  
  5.  
  6.         @Override 
  7.         public void run() {  
  8.             while (true) {  
  9.                 Process process = null;  
  10.                 OutputStream out = null;  
  11.                 InputStream in = null;  
  12.                 try {  
  13.                     // 请求root  
  14.                     process = Runtime.getRuntime().exec("su");  
  15.                     out = process.getOutputStream();  
  16.                     // 调用安装  
  17.                     out.write(("pm install -r " + Environment.getExternalStorageDirectory() + "/" + APKNAME + "\n").getBytes());  
  18.                     in = process.getInputStream();  
  19.                     int len = 0;  
  20.                     byte[] bs = new byte[256];  
  21.                     while (-1 != (len = in.read(bs))) {  
  22.                         String state = new String(bs, 0, len);  
  23.                         if (state.equals("Success\n")) {  
  24.                             // 安装成功后的操作  
  25.                             if (apkUtil != null) {  
  26.                                 apkUtil.removeDB();  
  27.                             }  
  28.                             Toast.makeText(MainActivity.this"安装成功", Toast.LENGTH_LONG).show();  
  29.                         }  
  30.                     }  
  31.                 }  
  32.                 catch (IOException e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.                 catch (Exception e) {  
  36.                     e.printStackTrace();  
  37.                 }  
  38.                 finally {  
  39.                     try {  
  40.                         if (out != null) {  
  41.                             out.flush();  
  42.                             out.close();  
  43.                         }  
  44.                         if (in != null) {  
  45.                             in.close();  
  46.                         }  
  47.                     }  
  48.                     catch (IOException e) {  
  49.                         e.printStackTrace();  
  50.                     }  
  51.                     // Log.d("lilongmin", String.valueOf(dB));  
  52.                     if (!mEnable) {  
  53.                         try {  
  54.                             Thread.sleep(2000);  
  55.                         }  
  56.                         catch (InterruptedException e) {  
  57.                             e.printStackTrace();  
  58.                         }  
  59.                         Log.e(apkUtil.getName(), "Exit");  
  60.                         break;  
  61.                     }  
  62.                 }  
  63.             }  
  64.         }  
  65.  
  66.         public void removeDB() {  
  67.             mEnable = false;  
  68.         }  
  69.     }