1. 程式人生 > >將apk打包成系統級應用並且使用靜默安裝重啟機器

將apk打包成系統級應用並且使用靜默安裝重啟機器


1. 在AndroidManifest.xml裡新增許可權    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />



2.使用root許可權進行靜默安裝
    public static boolean silentInstallApk(String filePath) {
        if (null == filePath || filePath.length() <= 0) {
            return false;
        }


        File file = new File(filePath);
        if (!file.exists()) {
            return false;
        }


        PrintWriter PrintWriter = null;
        Process process = null;
        int value = -1;


        try {
            // 先執行su命令,必須按照以下方式執行命令,否則會出現無許可權的問題。
            process = Runtime.getRuntime().exec("su");
            // 獲取輸入,
            PrintWriter = new PrintWriter(process.getOutputStream());
            // 寫入命令
            PrintWriter.println("chmod 777 "+ filePath);
            PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
            PrintWriter.println("pm install -r "+ filePath);
            // 退出
            PrintWriter.println("exit");
            PrintWriter.flush();
            PrintWriter.close();
            // 等待執行完畢
            value = process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(process!=null){
                process.destroy();
            }
        }


        return (0 == value);
    }


    
3.重啟機器
    /// 重啟
    public static boolean reboot() {
        PrintWriter PrintWriter = null;
        Process process = null;
        int value = -1;


        try {
            // 必須先執行su
            process = Runtime.getRuntime().exec("su");
            // 獲取輸入,然後再執行各個命令
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.println("reboot");
            PrintWriter.println("exit");
            PrintWriter.flush();
            PrintWriter.close();
            // 等待執行完成
            value = process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(process != null) {
                process.destroy();
            }
        }


        return (0 == value);
    }




4.將apk打包生成terminal-release.apk。


5.將apk使用 廠商提供的證書籤名,才能成為系統apk。
命令如下:
java -jar signApk.jar platform.x509.pem platform.pk8 shared.x509.pem shared.pk8 media.x509.pem media.pk8 terminal-release.apk terminal-release-sign.apk
生成 terminal-release-sign.apk
其中,signApk.jar/platform.x509.pem/platform.pk8/shared.x509.pem/shared.pk8/media.x509.pem/media.pk8是系統廠商提供的證書相關檔案。


參考:http://www.mobibrw.com/2015/2826


6.將terminal-release-sign.apk放在安卓機器的/system/app/目錄下,即系統級apk。