核心代码:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File(apkPath)),
"application/vnd.android.package-archive"
);
context.startActivity(intent);
核心代码:
Uri packageURI = Uri.parse("package:" + packageName);
Intent intent = new Intent(Intent.ACTION_DELETE, packageURI);
context.startActivity(intent);
上述代码中,packageName是目标APP的包名。
核心代码:
private static final String SILENT_INSTALL_CMD = "pm install -r ";
String installCmd = SILENT_INSTALL_CMD + apkPath;// PM指令不支持中文
int result = -1;
DataOutputStream dos = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(installCmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
result = process.exitValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(dos != null) {
dos.close();
}
if(process != null){
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
核心代码:
// 如果要保留数据,需要加-k参数,但是卸载会不完全
private static final String SILENT_UNINSTALL_CMD = "pm uninstall ";
String uninstallCmd = SILENT_UNINSTALL_CMD + appPackageName;
int result = -1;
DataOutputStream dos = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(uninstallCmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
result = process.exitValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(dos != null) {
dos.close();
}
if(process != null){
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
上述代码中,appPackageName是目标APP的包名。
更多内容可参考该页面内的install
、uninstall
、silentInstall
和silentUninstall
这四个方法。
原文地址:http://blog.csdn.net/risingwonderland/article/details/45537313