Android apk中安裝另一個apk

轉載只是爲了自己更好的查閱,原文章地址:http://www.eoeandroid.com/thread-561973-1-1.html

一種 發給系統 讓系統安裝  但是會出現 安裝界面  還得點擊  比較麻煩。 

一種 是 靜默安裝,  不瞭解的 百度一下就出來了。 這個安裝後,  如果手機安裝了 360  百度管家 等軟家  在通知欄會提示 軟件安裝成功。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />

因爲有些是系統權限  +進去後  manifest 會出錯  clean 一下 就可以了 。
要安裝的 apk 放到  assets 目錄下  安裝時  先將 目錄下的 apk 拷貝到 SD 卡上  改名爲 temp.apk  然後安裝temp.apk 

1.判斷是否已經安裝過此App ,android 裏面識別App的唯一方法是通過App的packetName 作爲唯一識別依據

 PackageInfo packageInfo;
                try {
                        packageInfo = getPackageManager().getPackageInfo(
                                        "com.example.imgrefocus", 0);
                } catch (NameNotFoundException e) {
                        packageInfo = null;
                        e.printStackTrace();

                }
                if (packageInfo == null) {
                        // 啓用安裝新線程
                        new Thread(new Runnable() {
                                @Override
                                public void run() {
                                        Log.e("hao", "未安裝進行安裝");
                                        slientInstall(); // 未安裝進行安裝                        
                                }
                        }).start();
                } else {
                        Log.e("hao", "已經安裝");
                }

2.靜默安裝

/**
         * 靜默安裝
         * 
         * @param file
         * @return
         */
        public boolean slientInstall() {
                createFile(); // 進行資源的轉移 將assets下的文件轉移到可讀寫文件目錄下
                File file = new File(Environment.getExternalStorageDirectory()
                                .getPath() + "/temp.apk");

                boolean result = false;
                Process process = null;
                OutputStream out = null;
                System.out.println(file.getPath());
                if (file.exists()) {
                        System.out.println(file.getPath() + "==");
                        try {
                                process = Runtime.getRuntime().exec("su");
                                out = process.getOutputStream();
                                DataOutputStream dataOutputStream = new DataOutputStream(out);
                                dataOutputStream.writeBytes("chmod 777 " + file.getPath()
                                                + "\n"); // 獲取文件所有權限
                                dataOutputStream
                                                .writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r "
                                                                + file.getPath()); // 進行靜默安裝命令
                                // 提交命令
                                dataOutputStream.flush();
                                // 關閉流操作
                                dataOutputStream.close();
                                out.close();
                                int value = process.waitFor();

                                // 代表成功
                                if (value == 0) {
                                        Log.e("hao", "安裝成功!");
                                        result = true;
                                } else if (value == 1) { // 失敗
                                        Log.e("hao", "安裝失敗!");
                                        result = false;
                                } else { // 未知情況
                                        Log.e("hao", "未知情況!");
                                        result = false;
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        if (!result) {
                                Log.e("hao", "root權限獲取失敗,將進行普通安裝");
                                Intent intent = new Intent();
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setAction(android.content.Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.fromFile(file),
                                                "application/vnd.android.package-archive");
                                startActivity(intent);
                                result = true;
                        }
                }

                return result;
        }

        public void createFile() {
                InputStream is = null;
                FileOutputStream fos = null;
                try {
                        is = MainActivity.this.getAssets().open("ImgRefocus.apk");
                        File file = new File(Environment.getExternalStorageDirectory()
                                        .getPath() + "/temp.apk");
                        file.createNewFile();
                        fos = new FileOutputStream(file);
                        byte[] temp = new byte[1024];
                        int i = 0;
                        while ((i = is.read(temp)) > 0) {
                                fos.write(temp, 0, i);
                        }
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } finally {
                        if (is != null) {
                                try {
                                        is.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                        if (fos != null) {
                                try {
                                        fos.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }

                }

        }

如果沒有root過的手機想要靜默安裝的話,目前"好像"是沒有辦法繞過的,如果每個軟件都能靜默安裝的話,Android手機就到處是病毒了,所以一般手機的廠商都會建議不要root手機,獲取手機的最高權限的,對手機小白來 說不安全

沒有root過的手機安裝的時候始終都會彈出安裝的提示,相當於用Intent 打開安裝的界面,然後把apk的安裝路徑傳遞過去

就然後就能安裝了,至於爲啥放在Asset目錄下面是因爲Asset目錄下面的文件是不會壓縮的,可以用來存放靜態的Sqlite 3的db 文件,gif圖片,音頻文件之類的,使用的時候只需要用文件流讀入到手機的內存卡中即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章