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 @ 2013-01-06 12:01  灰太狼_lilongmin  阅读(468)  评论(1编辑  收藏  举报