Android程序中安装APP
来源:互联网 发布:最新avtaobao域名 编辑:程序博客网 时间:2024/06/05 08:08
Android程序中安装APP
方法一
这种方法通过 Intent 机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据。
String fileName = Environment.getExternalStorageDirectory() + apkName ; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); startActivity(intent);
方法二
这种方法
- 需要对apk进行系统签名
- 需要系统权限,在manifest头部加上 android:sharedUserId=“android.uid.system”
- 需要安装权限 <uses-permission android:name=“android.permission.INSTALL_PACKAGES” />
- 稍微修改下命令就可以用于静默卸载
- 可以把apk push到/system/app目录,也可以直接安装,两种方式都有效
如果这个apk是系统应用( /system/app/ ),那么对它使用这种方式进行更新版本时,会在 /data/app/ 下面安装apk包,更新后的应用可以直接卸载,一旦卸载,在桌面上显示的就是原来的系统应用( /system/app/ )。对这个系统应用进行多次更新,更新后均会在/data/app目录下,而之前的包会被替换掉,在这个目录只会有一个该应用的包。
private String excuteCommand() {String result = "";ProcessBuilder processBuilder = new ProcessBuilder(args);Process process = null;InputStream errIs = null;InputStream inIs = null;try {ByteArrayOutputStream baos = new ByteArrayOutputStream();int read = -1;process = processBuilder.start();errIs = process.getErrorStream();while ((read = errIs.read()) != -1) {baos.write(read);}baos.write('\n');inIs = process.getInputStream();while ((read = inIs.read()) != -1) {baos.write(read);}byte[] data = baos.toByteArray();result = new String(data);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {try {if (errIs != null) {errIs.close();}if (inIs != null) {inIs.close();}} catch (IOException e) {e.printStackTrace();}if (process != null) {process.destroy();}}return result;}
//代码执行后,如果安装成功的话获取到的result值是“pkg: /data/local/tmp/test.apk /nSuccess”,如果是失败的话,则没有结尾的“Success”。
public static boolean installSilence(String apkPath) { String[] args = { "pm", "install", "-r", apkPath }; String result = excuteCommand(args); if (StringUtil.isEmpty(result)) { return false; } else { return result.lastIndexOf("Success") > 0; } }
卸载app
方法一
通过 Intent 机制,调出系统卸载应用。
Uri packageURI = Uri.parse("package:com.demo.test"); // 包名Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); startActivity(uninstallIntent);
方法二
直接调用卸载接口。
PackageInstallObserver observer = new PackageInstallObserver(); pm.installPackage( mPackageURI , observer, installFlags);//使用这种方法卸载应用需要权限: android.permission.DELETE_PACKAGES
0 0
- Android程序中安装APP
- Android程序中安装APP总结
- Android程序中安装APP总结
- Android中App安装位置
- 向iOS模拟器中安装.app程序
- android程序中 通过包名判断手机上是否已安装app
- 如何在自己的App中调用Android系统自带的安装/卸载程序
- Android-->安装程序(APK)后并启动程序(APP)
- Android中App安装位置详解
- Android中App安装位置详解
- Android中App安装位置详解
- Android:程序中安装程序APK
- Android安装和删除(卸载)应用软件程序(apk/app)
- android模拟器上安装和卸载app程序的方法
- Android 模拟器上app程序的安装和卸载
- Symbian 在一个运行的App中启动安装程序安装另外一个App
- 在Android模拟器(AVD)中安装Android应用 *.app
- APP程序打包与安装
- Jquery选择兄弟元素
- spring的@Transactional
- Zend Studio 修改高亮变量的颜色、括号颜色
- 机器学习框架、库
- Android UI 之一步步教你自定义控件(自定义属性、合理设计onMeasure、合理设计onDraw等)
- Android程序中安装APP
- iOS中 第三方LBXScan库二维码扫描
- Linux驱动学习笔记(2)----字符型设备驱动基本框架
- Sports
- 关于两个tabbar之见的跳转
- java提高篇-----异常(二)
- C/CPP点滴积累—数组名作为常量的错误典型
- 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴
- 文章标题