Android实现静默卸载
项目需要卸载自身app,总结卸载应用:
/*1. 有系统提示框,需要点击确认*/
Uri packageUri = Uri.parse("package:"+MainActivity.this.getPackageName()); Intent intent = new Intent(Intent.ACTION_DELETE,packageUri); startActivity(intent);
不希望出现提示框直接卸载,总结有以下几种,不过由于权限问题都需要提权为系统应用,清单文件中添加android:sharedUserId=”android.uid.system”
/*2. 执行adb命令速度较慢*/
String result = exec("pm uninstall com.example.hello");
private String exec(String command) {
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader render = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while((read = render.read(buffer)) > 0 ) {
output.append(buffer, 0 , read);
}
render.close();
process.waitFor();