-静默安装功能的实现

静默安装的实现比较简单,可是有个前提:html

你的应用必须有system权限。android

因此分为两步:app

1. 如何获取system权限。ide

    ①. 修改androidmanifest.xml ,       android:sharedUserId="android.uid.system"ui

    ②. 修改android.mk ,增长LOCAL_CERTIFICATE := platformspa

    ③. 编译便可,这样你的app就有了system权限。code

2. 如何静默安装。orm

    在这里我选择的是用Runtime.getRuntime().exec("pm install -r -s " );的方法。xml

    具体实现以下,能够调用这个方法来安装/system/installapp/下以1.apk  2.apk 命名的apk,并选择是否安装到SD卡。htm

  1. private int installapk_internel(int apkid, boolean sd) {  
  2.     ByteArrayOutputStream byteary = new ByteArrayOutputStream();  
  3.     ByteArrayOutputStream errbyteary = new ByteArrayOutputStream();  
  4.     InputStream input = null;  
  5.     InputStream errin = null;  
  6.     Process proc = null;  
  7.     int read = -1;  
  8.     int readerr= -1;  
  9.     String errresult= null;  
  10.     String result = null;  
  11.     try {  
  12.   
  13.         if (sd == true) {  
  14.             proc = Runtime.getRuntime().exec(  
  15.                     "pm install -r -s " + APK_DIR + Integer.toString(apkid)  
  16.                             + ".apk");  
  17.         } else {  
  18.             proc = Runtime.getRuntime().exec(  
  19.                     "pm install -r -f " + APK_DIR + Integer.toString(apkid)  
  20.                             + ".apk");  
  21.         }  
  22.         int exitVal = proc.waitFor();  
  23.         // wait for the proc to complete  
  24.         input = proc.getInputStream();  
  25.         errin = proc.getErrorStream();  
  26.   
  27.          while ((readerr = errin.read()) != -1) {  
  28.              errbyteary.write(readerr);  
  29.          }  
  30.     //  byteary.write('\n');  
  31.         while ((read = input.read()) != -1) {  
  32.             byteary.write(read);  
  33.         }  
  34.         byte data[] = byteary.toByteArray();  
  35.         byte dataerr[] = errbyteary.toByteArray();  
  36.         result = new String(data);  
  37.         errresult =new String(dataerr);  
  38.   
  39.         Log.d(TAG, "INSTALL  done ! id= " + apkid + " result code = "  
  40.                 + exitVal);  
  41.            Log.d(TAG,"INSTALL DONE ,err = "+errresult);  
  42.     } catch (IOException e) {  
  43.         // TODO Auto-generated catch block  
  44.         Log.d(TAG,"INSTALL  FAILD!");  
  45.         e.printStackTrace();  
  46.     } catch (InterruptedException e) {  
  47.         // TODO Auto-generated catch block  
  48.         e.printStackTrace();  
  49.   
  50.     } finally {  
  51.         if (input != null) {  
  52.             try {  
  53.                 input.close();  
  54.             } catch (IOException e) {  
  55.                 e.printStackTrace();  
  56.             }  
  57.         }  
  58.         if (errin != null) {  
  59.             try {  
  60.                 errin.close();  
  61.             } catch (IOException e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.         }  
  65.         if (proc != null) {  
  66.   
  67.             proc.destroy();  
  68.         }  
  69.         Log.d(TAG, " install  complete, result = " + result);  
  70.     }  
  71.     if (result != null&&errresult != null) {  
  72.         return handle_install_result(result,errresult);  
  73.     } else {  
  74.         return -3;  
  75.     }  
  76. }  
这里还增长了对 pm install 的结果的处理 

  1. private static final String APK_DIR = "/system/installapp/";  
  2. private static final int INSTALL_APK_SUCESS = 0;  
  3. private static final int INSTALL_APK_FAILED_SD_CARD_NOT_PRESENT = -1;  
  4. private static final int INSTALL_APK_FAILED_INTENER_MEMORY_FULL = -2;  
  5. private static final int INSTALL_APK_FAILED_OTHER = -3;  
  6. private static final String INSTALL_RESULT_SUCCESS = "Success";  
  7. private static final String INSTALL_RESULT_SD_NOT_PRESENT = "Failure [INSTALL_FAILED_MEDIA_UNAVAILABLE]";  


  1. private int handle_install_result(String result,String errresult) {  
  2.     if (result.contains(INSTALL_RESULT_SUCCESS)) {  
  3.         return INSTALL_APK_SUCESS;  
  4.     } else if (errresult.contains(INSTALL_RESULT_SD_NOT_PRESENT)) {  
  5.         return INSTALL_APK_FAILED_SD_CARD_NOT_PRESENT;  
  6.     } else {  
  7.         return INSTALL_APK_FAILED_OTHER;  
  8.     }  
  9.   
  10. }  
这样咱们能够在安装到sd失败的状况下尝试安装到内存:
  1. int ret = installapk_internel(current_id, true);  
  2. if (ret == INSTALL_APK_FAILED_SD_CARD_NOT_PRESENT) {  
  3.     // then we try to install apks to internel memory.  
  4.     Log.d(TAG,  
  5.             "sdcard not present, we try to install apk to internel flash .");  
  6.     ret = installapk_internel(current_id, false);  
  7.     Log.d(TAG,"ret= "+ret);  
  8.     if (ret != INSTALL_APK_SUCESS) {  
  9.         Log.d(TAG, "install faild again.");  
  10.         // WriteInstall_FAILD_FlagtoFile(current_id);  
  11.     }  
  12. }  


总的来讲,静默安装仍是比较简单的,与你们分享下,共同进步。