silent Install APK file - Android App

Android examples for App:APK Install and Uninstall

Description

silent Install APK file

Demo Code


//package com.java2s;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

public class Main {

    public static boolean silentInstall(File file) {
        if (file == null) {
            return true;
        }//from w w  w .ja  v  a2  s  .c o m
        boolean result = false;
        Process process = null;
        OutputStream out = null;
        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) {
                result = true;
            } else if (value == 1) { // ??
                result = false;
            } else { // ????
                result = false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return result;
    }
}

Related Tutorials