• Android静默安装


    静默安装的两种方式

    1、需要root权限

    2、拥有系统签名

    首先第一种方式:

    需要root权限

     1 import java.io.File;
     2 import java.io.IOException;
     3 import java.io.InputStream;
     4 import java.io.OutputStream;
     5 import android.content.Context;
     6 
     7 public class SleepInstall {
     8 
     9     public static boolean installFile(File path,Context context){
    10         boolean result = false;
    11         Process process = null;
    12         OutputStream out = null;
    13         InputStream in = null;
    14         
    15         try {
    16             // 请求root
    17             process = Runtime.getRuntime().exec("su");
    18             out = process.getOutputStream();
    19 
    20             // 调用安装,将文件写入到process里面
    21             out.write(("pm install -r " + path + "
    ").getBytes());
    22             // 这里拿到输出流,开始安装操作
    23             in = process.getInputStream();
    24 
    25         } catch (IOException e) {
    26             e.printStackTrace();
    27         } catch (Exception e) {
    28             e.printStackTrace();
    29         } finally {
    30             try {
    31                 if (out != null) {
    32                     out.flush();
    33                     out.close();
    34                 }
    35                 if (in != null) {
    36                     in.close();
    37                 }
    38             } catch (IOException e) {
    39                 e.printStackTrace();
    40             }
    41         }
    42         return result;
    43     }
    44 
    45 }

    第二种方式:系统签名

    这种方式代码比较简单,但是用到了隐藏的api,并且需要在源码环境下编译

    1 if (file.exists()) {
    2                 getPackageManager().installPackage(Uri.fromFile(file), null,PackageManager.INSTALL_REPLACE_EXISTING,"com.nihao.com");
    3             }

    file就是你要安装的apk文件,com.nihao.com是你要安装的apk文件的包名

  • 相关阅读:
    springcloud之配置中心和消息总线(配置中心终结版)
    yaml
    RESTful API
    单元测试Junit5
    IDEA社区版创建web项目
    Mybatis常见面试题
    mybatis逆向工程
    mybatis注解
    延迟加载
    缓存
  • 原文地址:https://www.cnblogs.com/all88/p/3540478.html
Copyright © 2020-2023  润新知