install Apk Silently - Android android.content.pm

Android examples for android.content.pm:Apk Install

Description

install Apk Silently

Demo Code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

import android.content.Context;

public class Main {

  public static boolean installApkSilently(Context context, String path, int option) throws IOException {
    boolean result = false;
    Process process = null;//from   ww w .  j a  v a  2 s  .c  o  m
    OutputStream out = null;
    InputStream in = null;
    String state = null;
    String installOption = String.format(Locale.getDefault(), " setInstallLocation %d", option);

    process = Runtime.getRuntime().exec("su");
    out = process.getOutputStream();
    out.write(("pm install -r " + path + installOption + "\n").getBytes());
    in = process.getInputStream();
    int len = 0;
    byte[] bs = new byte[256];
    while (-1 != (len = in.read(bs))) {
      state = new String(bs, 0, len);
      if (state.equals("Success\n")) {
        result = true;
        break;
      }
    }
    if (out != null) {
      out.flush();
      out.close();
    }
    if (in != null) {
      in.close();
    }

    return result;
  }

}

Related Tutorials