这篇让我们来看下如何在android N 上使用pm命令实现静默安装。在android低版本上使用pm命令实现静默安装是可以的,但是到了android N 上使用就不行了。那android N上要怎么实现呢。
在Android应用端实现静默安装
1、实现pm命令
String cmd = "pm install -r -d /data/data/android.apk"
Runtime run = Runtime.getRuntime();
Process process = run.exec(cmd);
-d 是降版本安装的参数
2、在Androidmanifest中配置系统签名
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.xxx.xxxx"
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
在android N中实现静默安装
public void install(File apkFile) {
String cmd = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
cmd = "pm install -r -d " + apkFile.getAbsolutePath();
} else {
cmd = "pm install -r -d -i packageName --user 0 " + apkFile.getAbsolutePath();
}
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(cmd);
InputStream errorInput = process.getErrorStream();
InputStream inputStream = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String error = "";
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader = new BufferedReader(new InputStreamReader(errorInput));
while ((line = bufferedReader.readLine()) != null) {
error += line;
}
if(result.equals("Success")){
Log.i(TAG, "install: Success");
}else{
Log.i(TAG, "install: error"+error);
}
} catch (IOException e) {
e.printStackTrace();
}
}
最后记得系统签名。然后就能在android N版本中实现静默安装了。