Android_如何靜默安裝

Android常用代碼之普通及系統權限靜默安裝APK

本文主要介紹程序如何安裝apk,包括普通模式安裝和系統權限靜默安裝。

如果是非系統應用請直接查看:Android常用代碼之APK root權限靜默安裝,查看更完美的解決方案。

 

1、普通模式安裝,調用系統Intent,代碼如下:

public static void install(Context context, String filePath) {

    Intent i = new Intent(Intent.ACTION_VIEW);

    i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(i);

}

2、靜默安裝

如果是非系統應用請移步:Android常用代碼之APK root權限靜默安裝,查看更完美的解決方案。

分爲如下三步

(1)、靜默安裝需要系統應用安裝權限,需要在AndroidManifest.xml中添加

Java


1

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

 記得clear啊 一般會出錯提示。

(2)、實現代碼

靜默安裝代碼如下,實際是通過pm install -r 命令安裝。

注意:該函數最好在新建的線程中運行並通過handler發送安裝結果給主線程,否則安裝時間較長會導致ANR。

/**

 * install slient

 * 

 * @param context

 * @param filePath

 * @return 0 means normal, 1 means file not exist, 2 means other exception error

 */

public static int installSlient(Context context, String filePath) {

    File file = new File(filePath);

    if (filePath == null || filePath.length() == 0 || (file = new File(filePath)) == null || file.length() <= 0

        || !file.exists() || !file.isFile()) {

        return 1;

    }

 

    String[] args = { "pm", "install", "-r", filePath };

    ProcessBuilder processBuilder = new ProcessBuilder(args);

 

    Process process = null;

    BufferedReader succe***esult = null;

    BufferedReader errorResult = null;

    StringBuilder successMsg = new StringBuilder();

    StringBuilder errorMsg = new StringBuilder();

    int result;

    try {

        process = processBuilder.start();

        succe***esult = new BufferedReader(new InputStreamReader(process.getInputStream()));

        errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        String s;

 

        while ((s = succe***esult.readLine()) != null) {

            successMsg.append(s);

        }

 

        while ((s = errorResult.readLine()) != null) {

            errorMsg.append(s);

        }

    } catch (IOException e) {

        e.printStackTrace();

        result = 2;

    } catch (Exception e) {

        e.printStackTrace();

        result = 2;

    } finally {

        try {

            if (succe***esult != null) {

                succe***esult.close();

            }

            if (errorResult != null) {

                errorResult.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        if (process != null) {

            process.destroy();

        }

    }

 

    // TODO should add memory is not enough here

    if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {

        result = 0;

    } else {

        result = 2;

    }

    Log.d("installSlient", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);

    return result;

}

返回值0表示安裝成功,1表示文件不存在,2表示其他錯誤。需要更豐富的安裝失敗信息(內存不足、解析包出錯)可直接使用PackageUtils.installSlient。

 

(3) 、獲取系統權限

完成了上面兩步後,普通方式安裝我們的應用仍然無法靜默安裝。需要我們的應用獲得系統權限,編譯應用並通過

adb push <your_apk_path> /system/app/

命令實現安裝,即可擁有系統權限。


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