apk静默安装

复制代码
    // apk静默安装
    private class ApkUtil
        extends Thread {
        private boolean mEnable = true;

        @Override
        public void run() {
            while (true) {
                Process process = null;
                OutputStream out = null;
                InputStream in = null;
                try {
                    // 请求root
                    process = Runtime.getRuntime().exec("su");
                    out = process.getOutputStream();
                    // 调用安装
                    out.write(("pm install -r " + Environment.getExternalStorageDirectory() + "/" + APKNAME + "\n").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\n")) {
                            // 安装成功后的操作
                            if (apkUtil != null) {
                                apkUtil.removeDB();
                            }
                            Toast.makeText(MainActivity.this, "安装成功", Toast.LENGTH_LONG).show();
                        }
                    }
                }
                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();
                    }
                    // Log.d("lilongmin", String.valueOf(dB));
                    if (!mEnable) {
                        try {
                            Thread.sleep(2000);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e(apkUtil.getName(), "Exit");
                        break;
                    }
                }
            }
        }

        public void removeDB() {
            mEnable = false;
        }
    }
复制代码

 

posted @   灰太狼_lilongmin  阅读(469)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2012-01-06 ScrollView隐藏、调整大小
点击右上角即可分享
微信分享提示