android静默安装

来源:互联网 发布:数据库联表的算法思想 编辑:程序博客网 时间:2024/05/16 11:38

签名流程:

前提: 拿到系统的签名文件platform.x509.pem 和 platform.pk8,同时找到signapk.jar工具包(android源码中有对应类,可以拿到源码后

手动生成jar文件)

具体步骤如下:
 1. 将下载完毕的apk文件重新签名,文件签名和系统签名保存一致。 
            java -jar signapk.jar platform.x509.pem platform.pk8 待签名.apk 已签名.apk
 
2. 执行"pm", "install", "-r", apkInstallPath。开始安装。
  附代码:

[java] view plaincopy
  1. /**Android静默安装实现*/  
  2.  public static void silenceInstall(String apkInstallPath) {  
  3.   String[] args = { "pm""install""-r", apkInstallPath + "mobile_client_2.1.apk" };  
  4.   String result = "";  
  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.      
  19.    inIs = process.getInputStream();  
  20.    while ((read = inIs.read()) != -1) {  
  21.     baos.write(read);  
  22.    }  
  23.      
  24.    byte[] data = baos.toByteArray();  
  25.    result = new String(data);  
  26.   } catch (IOException e) {  
  27.    e.printStackTrace();  
  28.   } catch (Exception e) {  
  29.    e.printStackTrace();  
  30.   } finally {  
  31.    try {  
  32.     if (errIs != null) errIs.close();  
  33.     if (inIs != null) inIs.close();  
  34.    } catch (IOException e) {  
  35.     e.printStackTrace();  
  36.    }  
  37.      
  38.    if(process != null) process.destroy();  
  39.   }  
  40.   Log.d("mylog""执行静默安装后的返回值:" + result);  
  41.  }  

 

 

================================================================================

在代码中实现签名:

[java] view plaincopy
  1. /** 
  2.      * apk文件签名实现 
  3.      * @param apkPrePath    签名前的文件路径 
  4.      * @param apkCurPath    生成签名后的文件路径 
  5.      */  
  6.     public void signToApk(String apkPrePath, String apkCurPath) {  
  7.         Toast.makeText(MainActivity.this, apkPrePath, Toast.LENGTH_SHORT).show();  
  8.           
  9.         String[] args = { "java""-jar", apkPrePath + "signapk.jar", apkPrePath + "platform.x509.pem.pem", apkPrePath + "platform.pk8.pk8", apkPrePath + "mobile_360_client_2.1.apk", apkPrePath + "mobile_360_client_2.1_cur.apk"};  
  10.         String result = "";  
  11.         ProcessBuilder processBuilder = new ProcessBuilder(args);  
  12.         Process process = null;  
  13.         InputStream errIs = null;  
  14.         InputStream inIs = null;  
  15.         try {  
  16.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  17.             int read = -1;  
  18.             process = processBuilder.start();  
  19.             errIs = process.getErrorStream();  
  20.             while ((read = errIs.read()) != -1) {  
  21.                 baos.write(read);  
  22.             }  
  23.             baos.write('\n');  
  24.               
  25.             inIs = process.getInputStream();  
  26.             while ((read = inIs.read()) != -1) {  
  27.                 baos.write(read);  
  28.             }  
  29.               
  30.             byte[] data = baos.toByteArray();  
  31.             result = new String(data);  
  32.         } catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         } finally {  
  37.             try {  
  38.                 if (errIs != null) errIs.close();  
  39.                 if (inIs != null) inIs.close();  
  40.             } catch (IOException e) {  
  41.                 e.printStackTrace();  
  42.             }  
  43.               
  44.             if(process != null) process.destroy();  
  45.         }  
  46.     }  


如果是在命令行中生成签名, 则直接在cmd窗口中输入java -jar signapk.jar platform.x509.pem platform.pk8 待签名.apk 已签名.apk即可。

 

0 0
原创粉丝点击