apk 静默安装

 

  1. /** 
  2.      * 软件静默安装 
  3.      * @param apkAbsolutePath apk文件所在路径 
  4.      * @return 安装结果:获取到的result值<br> 
  5.      *  
  6.      * 如果安装成功的话是“ 
  7.      * pkg: /data/local/tmp/Calculator.apk  /nSuccess”,<br> 
  8.      * 如果是失败的话,则没有结尾的“Success”。 
  9.      */  
  10.     public String silentInstall(String apkAbsolutePath) {  
  11.         String[] args = { "pm", "install", "-r", apkAbsolutePath };  
  12.         String result = "";  
  13.         ProcessBuilder processBuilder = new ProcessBuilder(args);  
  14.         Process process = null;  
  15.         InputStream errIs = null;  
  16.         InputStream inIs = null;  
  17.   
  18.         try {  
  19.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  20.             int read = -1;  
  21.             process = processBuilder.start();  
  22.             errIs = process.getErrorStream();  
  23.   
  24.             while ((read = errIs.read()) != -1) {  
  25.                 baos.write(read);  
  26.             }  
  27.   
  28.             baos.write("/n".getBytes());  
  29.             inIs = process.getInputStream();  
  30.   
  31.             while ((read = inIs.read()) != -1) {  
  32.                 baos.write(read);  
  33.             }  
  34.   
  35.             byte[] data = baos.toByteArray();  
  36.             result = new String(data);  
  37.         } catch (IOException e) {  
  38.             e.printStackTrace();  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.         } finally {  
  42.             try {  
  43.                 if (errIs != null) {  
  44.                     errIs.close();  
  45.                 }  
  46.                 if (inIs != null) {  
  47.                     inIs.close();  
  48.                 }  
  49.             } catch (IOException e) {  
  50.                 e.printStackTrace();  
  51.             }  
  52.             if (process != null) {  
  53.                 process.destroy();  
  54.             }  
  55.         }  
  56.         return result;  
  57.     }  

 

别急,没完,记得加权限呢:

 

  1. <!-- 以下是静默安装apk所需要到权限 -->  
  2.     <uses-permission android:name="android.permission.INSTALL_PACKAGES" />  
  3.     <uses-permission android:name="android.permission.DELETE_PACKAGES" />  
  4.     <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />  
  5.     <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />  
  6.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  

 

我粘贴过去,怎么回事,鼠标移上去一看,

  1. permission is only granted to system apps   

 

posted @ 2014-12-23 09:48  东方小虾米  阅读(132)  评论(0编辑  收藏  举报