安裝APK文件
方法一:傳入File文件
public void installApp(File file) {
try {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
方法二:傳入文件名稱
public void installApp(String file_name) {
try {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.parse(file_name), type);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
卸載APK
Uri uri = Uri.parse("package:com.xxx.xxx");
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
Uri解析的內容必須爲“package:包名”,利用ACTION_DELETE動作和Uri數據,就可以卸載了。
在安裝和卸載時,並不需要另外在AndroidManifest.xml文件添加權限。