Android 백그라운드 침묵 설치

1831 단어 Android
배경 에 조용히 설치 하면 코드 호출 시스템 의 PackageInstaller 인 터 페 이 스 를 쓰 지 않 고 명령 을 직접 사용 합 니 다.cmd 에서 adb install 을 직접 실행 하 는 것 과 달리 코드 에 pm 라 는 시스템 bin 파일 을 사용 해 야 합 니 다.
    private void startUpdate() {

        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder errorMsg = new StringBuilder();
        try {
            **// 7.0          
            //           "-i", "      ", 
            //     ,         android.permission.INSTALL_PACKAGES   **
            process = new ProcessBuilder("pm", "install", "-i", "      ", "-r", "sdcard/test.apk").start();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s = successResult.readLine()) != null) {
                successMsg.append(s);
            }
            while ((s = errorResult.readLine()) != null) {
                errorMsg.append(s);
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception " + e.toString());
        } finally {
            try {
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (Exception e) {

            }
            if (process != null) {
                process.destroy();
            }
        }
        Log.e(TAG, "errorMsg " + errorMsg.toString());
        Log.d(TAG, "successMsg " + successMsg.toString());

    }

좋은 웹페이지 즐겨찾기