zoukankan      html  css  js  c++  java
  • Android 静默安装

     有时候我们需要软件实现静默安装,但是Android并未提供相应的API,然而我们知道命令行安装android的时候是不会提示用户的,所有要实现这个功能,我们就可以从执行命令行的方式实现。android提供的安装命令是

    1. pm install package 
    2. ps : pm install /sdcard/android.apk 
    ?

     

    但是执行这个命令需要前提条件,需要是系统级应用或者具有ROOT权限。我们先介绍通过ROOT权限的方式执行。

    1,通过获取ROOT权限静默安装APK
    看代码:

        new Thread() { 
            public void run() { 
            Process process = null; 
            OutputStream out = null; 
            InputStream in = null; 
            try { 
            // 请求root 
            process = Runtime.getRuntime().exec("su");  
            out = process.getOutputStream(); 
            // 调用安装 
            out.write(("pm install -r " + currentTempFilePath + "
    ").getBytes()); 
            in = process.getInputStream(); 
            int len = 0; 
            byte[] bs = new byte[256]; 
            while (-1 != (len = in.read(bs))) { 
            String state = new String(bs, 0, len); 
            if (state.equals("Success
    ")) { 
               //安装成功后的操作 
                 } 
               } 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } finally { 
                try { 
                    if (out != null) { 
                        out.flush(); 
                        out.close(); 
                    } 
                    if (in != null) { 
                        in.close(); 
                    } 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
          } 
        }.start(); 

    当然也可以通过NDK实现,代码就不放了。

    第二种方式,同样是通过pm命令实现,不用请求ROOT,但是需要系统的签名。这里附上模拟器的的签名,用这个签名的APK安装在模拟器上可以实现不请求ROOT而静默安装的效果。

    ?
        //首先在manifest标签加入属性 
        android:sharedUserId="android.uid.system" 
    
        new Thread() { 
            public void run() { 
            Process process = null; 
            InputStream in = null; 
            try { 
            // 请求root 
            process = Runtime.getRuntime().
        exec("pm install -r " + currentTempFilePath + "
    ");  
            in = process.getInputStream(); 
            int len = 0; 
            byte[] bs = new byte[256]; 
            while (-1 != (len = in.read(bs))) { 
            String state = new String(bs, 0, len); 
            if (state.equals("Success
    ")) { 
               //安装成功后的操作 
                 } 
               } 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } finally { 
                try { 
                    if (in != null) { 
                        in.close(); 
                    } 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
          } 
        }.start(); 
  • 相关阅读:
    grep 命令操作
    vi & vim复制,粘贴,剪切文本
    β版本第四次冲刺
    β版本第三次冲刺
    β版本第二次冲刺
    β版本第一次冲刺
    华为软件开发云,个人评测及体会
    事后诸葛亮
    【Alpha】团队课程展示
    团队Alpha博客链接目录
  • 原文地址:https://www.cnblogs.com/blogzhangwei/p/4081187.html
Copyright © 2011-2022 走看看