Android-コマンドラインスクリプトの実行
:
Runtime.getRuntime().exec(cmd);
方式2:
まずString配列を1つ入れ、スペースを、番号に代えてcmdを記入する.サイレントインストールに関してはroot権限が必要です
String[] args = {"pm", "install", "-t", "-r", apkPath, "--user", "0"};
exeCmdArgs(args);
//
private static void exeCmdArgs(String[] args)
throws Exception {
ByteArrayOutputStream errorBuffer = new ByteArrayOutputStream();
ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();
ProcessBuilder processBuilder = null;
Process process = null;
InputStream errorInput = null;
InputStream resultInput = null;
int byteOfRead = 0;
byte[] buffer = new byte[1024];
try {
processBuilder = new ProcessBuilder(args);
process = processBuilder.start();
errorInput = process.getErrorStream();
while (-1 != (byteOfRead = errorInput.read(buffer))) {
errorBuffer.write(buffer, 0, byteOfRead);
}
resultInput = process.getInputStream();
while (-1 != (byteOfRead = resultInput.read(buffer))) {
resultBuffer.write(buffer, 0, byteOfRead);
}
String error = errorBuffer.toString("UTF-8");
String result = resultBuffer.toString("UTF-8");
System.out.println(error);
System.out.println(result);
} finally {
//
close(errorInput, resultInput);
destroy(process);
}
}