靜默安裝
***First blood ***
遇到了靜默安裝需求,普通的手機是需要root權(quán)限的,root后即可以使用pm命令安裝,也可以簽名為系統(tǒng)應(yīng)用后即可使用pm命令靜默安裝,系統(tǒng)簽名命令如下:
set apkFile=%1
set signedFile=%apkFile%_signed.apk
java -jar signapk.jar platform.x509.pem platform.pk8 %apkFile% %signedFile%
這段命令可直接在windows下存儲為bat下次直接把文件拖入即可簽名
需要的文件:signapk.jar,platform.x509.pem,platform.pk8
下載鏈接
注:看手機型號而定,國內(nèi)的手機型號比較ken...
在網(wǎng)上找了下資料,記錄如下:
具體實現(xiàn)
static DataOutputStream dataOutputStream = null;
static BufferedReader errorStream = null;
static Process process;
static boolean result = false;
public static boolean slientInstall(String apkPath)
{
try {
process = Runtime.getRuntime().exec("su"); //獲取su權(quán)限
dataOutputStream = new DataOutputStream(process.getOutputStream());//獲取
String command = "pm install -r "+apkPath+"\n"; //安裝命令
String path = apkPath;
Log.e("Tag",command);
Log.e("Tag",path+"+++>path");
dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
dataOutputStream.flush();
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
try {
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); //記錄安裝的信息
String msg = "";
String line;
while ((line = errorStream.readLine())!= null)
{
msg+=line;
}
Log.e("Tag","安裝的信息是"+msg);
if (!msg.contains("Failure"))
{
return true;
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}finally {
try {
if (dataOutputStream!=null){
dataOutputStream.close();
}
if (errorStream!= null){
errorStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
上面就實現(xiàn)了靜默安裝的功能.
記得加上權(quán)限:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />