静默安装和卸载第三方apk

1.实现静默安装和卸载需要两个权限

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

<uses-permission android:name="android.permission.DELETE_PACKAGES" />
2.你的app需要放置在System\app或者System\priv-app目录下,也就是说要有root权限或者是系统应用才行

3.我都是选择adb命令行的方式,我看见有的博客是用反射来做的,不过我是试过了,在API25的情况下我没跑通,可能是我自己的问题。废话不多说上代码:

 (1)静默安装:

  1.   private boolean install()  
  2.   {  
  3.     String[] args = {"pm""install""-r", packageUri.getPath()};  
  4. String result = null;  
  5. ProcessBuilder processBuilder = new ProcessBuilder(args);;  
  6. Process process = null;  
  7. InputStream errIs = null;  
  8. InputStream inIs = null;  
  9. try{  
  10.     ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  11.     int read = -1;  
  12.     process = processBuilder.start();  
  13.     errIs = process.getErrorStream();  
  14.     while((read = errIs.read()) != -1){  
  15.         baos.write(read);  
  16.     }  
  17.     baos.write('\n');  
  18.     inIs = process.getInputStream();  
  19.     while((read = inIs.read()) != -1){  
  20.         baos.write(read);  
  21.     }  
  22.     byte[] data = baos.toByteArray();  
  23.     result = new String(data);  
  24. }catch(Exception e){  
  25.     e.printStackTrace();  
  26. }finally{  
  27.     try{  
  28.         if(errIs != null){  
  29.             errIs.close();  
  30.         }  
  31.         if(inIs != null){  
  32.             inIs.close();  
  33.         }  
  34.     }catch(Exception e){  
  35.         e.printStackTrace();  
  36.     }  
  37.     if(process != null){  
  38.         process.destroy();  
  39.     }  
  40. }  
  41.   
  42. if(result != null && (result.endsWith("Success")||result.endsWith("Success\n")))  
  43. {  
  44.     return true;  
  45. }  
  46. return false;  
  47.   }  

(2)静默卸载:
  1. private void uninstall2() {  
  2.         String[] args = { "pm""uninstall""com.popcap.pvzthird" };  
  3.         String result = null;  
  4.         ProcessBuilder processBuilder = new ProcessBuilder(args);  
  5.         ;  
  6.         Process process = null;  
  7.         InputStream errIs = null;  
  8.         InputStream inIs = null;  
  9.         try {  
  10.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  11.             int read = -1;  
  12.             process = processBuilder.start();  
  13.             errIs = process.getErrorStream();  
  14.             while ((read = errIs.read()) != -1) {  
  15.                 baos.write(read);  
  16.             }  
  17.             baos.write('\n');  
  18.             inIs = process.getInputStream();  
  19.             while ((read = inIs.read()) != -1) {  
  20.                 baos.write(read);  
  21.             }  
  22.             byte[] data = baos.toByteArray();  
  23.             result = new String(data);  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         } finally {  
  27.             try {  
  28.                 if (errIs != null) {  
  29.                     errIs.close();  
  30.                 }  
  31.                 if (inIs != null) {  
  32.                     inIs.close();  
  33.                 }  
  34.             } catch (Exception e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.             if (process != null) {  
  38.                 process.destroy();  
  39.             }  
  40.         }  
  41.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值