Firefly开源社区

[Android] [风之空响][经验分享]Android静默安装应用

741

积分

68

威望

68

贡献

技术大神

Rank: 3Rank: 3

积分
741
QQ
发表于 2018-11-8 18:02:18     
本帖最后由 风之空响 于 2018-11-9 00:07 编辑

1.实现静默安装应用的话需要系统权限:
  • 在AndroidManifest.xml中添加android:sharedUserId="android.uid.system".
  • 使用系统签名文件(platform.pk8、platform.x509.pem,签名文件位于build/target/product/security/目录下)对apk进行签名


2.在AndroidManifest.xml添加相应需要的权限
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


3.添加函数代码,调用函数进行应用安装
  1. int result = installSilent("com.firefly.testslinet","/mnt/sdcard/123.apk");
复制代码

前面包名是你实现静默安装的应用的包名,比如我的是testslinet 应用来静默安装/sdcrad/123.apk.那么就是installSilent("com.firefly.testslinet","/mnt/sdcard/123.apk");


  1.         /**
  2.          * 静默安装
  3.          * @param packageName   调用installSilent函数的应用包名
  4.          * @param filePath     静默安装应用的apk路径
  5.          * @return 0 安装成功
  6.          *         1 文件不存在
  7.          *         2 安装失败
  8.          */  
  9.         public static int installSilent(String packageName,String filePath) {  
  10.                 File file = new File(filePath);  
  11.                 if (filePath == null || filePath.length() == 0 || file == null || file.length() <= 0 || !file.exists() || !file.isFile()) {  
  12.                         return 1;  
  13.                 }  
  14.                 //pm install -i 包名 --user 0 apkpath
  15.                 String[] args = { "pm", "install","-i",packageName,"--user","0", "-r", filePath };  
  16.                 ProcessBuilder processBuilder = new ProcessBuilder(args);  
  17.                 Process process = null;  
  18.                 BufferedReader successResult = null;  
  19.                 BufferedReader errorResult = null;  
  20.                 StringBuilder successMsg = new StringBuilder();  
  21.                 StringBuilder errorMsg = new StringBuilder();  
  22.                 int result;  
  23.                 try {  
  24.                         process = processBuilder.start();  
  25.                         successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));  
  26.                         errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));  
  27.                         String s;
  28.                         Log.d("installSilent", "1");  
  29.                         while ((s = successResult.readLine()) != null) {  
  30.                                 successMsg.append(s);
  31.                                 Log.d("installSilent", "while successMsg s:" + s);  
  32.                         }
  33.                         Log.d("installSilent", "2");  
  34.                         while ((s = errorResult.readLine()) != null) {  
  35.                                 errorMsg.append(s);  
  36.                                 Log.d("installSilent", "while errorMsg s:" + s);  
  37.                         }  
  38.                         Log.d("installSilent", "3");  
  39.                 } catch (IOException e) {  
  40.                         Log.d("installSilent", "4");  
  41.                         e.printStackTrace();  
  42.                 } catch (Exception e) {  
  43.                         Log.d("installSilent", "5");  
  44.                         e.printStackTrace();  
  45.                 } finally {  
  46.                         Log.d("installSilent", "6");  
  47.                         try {  
  48.                                 if (successResult != null) {  
  49.                                         successResult.close();  
  50.                                 }  
  51.                                 if (errorResult != null) {  
  52.                                         errorResult.close();  
  53.                                 }  
  54.                         } catch (IOException e) {  
  55.                                 e.printStackTrace();  
  56.                         }  
  57.                         if (process != null) {  
  58.                                 process.destroy();  
  59.                         }  
  60.                 }  

  61.                 if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {  
  62.                         result = 0;  
  63.                 } else {  
  64.                         result = 2;  
  65.                 }  
  66.                 Log.d("installSilent", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);  
  67.                 return result;  
  68.         }
复制代码









回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表