Android对APK文件的安装和卸载
16lz
2021-01-23
安装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文件添加权限。
更多相关文章
- android aidl文件分析
- 在Mac上完全删除android studio初始配置文件
- Android中几种延后处理事件的方法
- Android判断包名和类名是否存在的方法
- Android SDK 1.5 "--core-library" build error问题解决方法
- android http通信(二) 英译汉字典实例探究网络通信三种方法
- android sdk content loader 加载很慢的解决方法
- android 开发中的文件操作