-静默安装功能的实现

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

你的应用必须有system权限。

所以分为两步:

1. 如何获取system权限。

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

    ②. 修改android.mk ,增加LOCAL_CERTIFICATE := platform

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

2. 如何静默安装。

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

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

  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. }  


总的来说,静默安装还是比较简单的,与大家分享下,共同进步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值