android 实现静默安装和卸载

通过获取系统root权限调用pm install -r apkpath 和pm uninstall pkgname 来实现.

附上代码:

// install apk
	private static void install(Activity activity, String path) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(new File(path)),
				"application/vnd.android.package-archive");
		activity.startActivity(intent);
	}

	// uninstall a apk
	private static void uninstall(Activity activity, String pkg) {
		if (pkg == null) {
			return;
		}
		Uri uri = Uri.fromParts("package", pkg, null);
		Intent intent = new Intent(Intent.ACTION_DELETE, uri);
		activity.startActivity(intent);
	}

	// install
	public static void install_root(final Activity activity,final String path, final int msgwhat, final Handler callback) {
		new Thread() {
			public void run() {
				Process process = null;
				OutputStream out = null;
				InputStream in = null;
				try {
					// 请求root
					process = Runtime.getRuntime().exec("su");
					if(callback!=null){
						Message msg = new Message();
						msg.what = msgwhat;
						msg.obj = 0; //安装开始
						callback.sendMessage(msg);
					}
					out = process.getOutputStream();
					// 调用安装
					out.write(("pm install -r " + path + "\n").getBytes());
					in = process.getInputStream();
					byte[] bs = new byte[256];
					int len = in.read(bs);
					if (-1 != len) {
						//取得root成功 
						String state = new String(bs, 0, len);
						SLog.e("test", "state = "+state);
						if (state.equals("Success\n")) {
							if(callback!=null){
								Message msg = new Message();
								msg.what = msgwhat;
								msg.obj = 1; //安装成功
								callback.sendMessage(msg);
							}
						}else {
							if(callback!=null){
								Message msg = new Message();
								msg.what = msgwhat;
								msg.obj = 2; //安装失败
								callback.sendMessage(msg);
							}
						}
					}else{
						//取得root失败
						install(activity,path);
					}
						
				} catch (IOException e) {
					SLog.e("test", "IOException 1111 = ");
					e.printStackTrace();
				} catch (Exception e) {
					SLog.e("test", "Exception 1111 = ");
					e.printStackTrace();
				} finally {
					try {
						if (out != null) {
							out.flush();
							out.close();
						}
						if (in != null) {
							in.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	public static void uninstall_root(final Activity activity,final String pkg,final int msgwhat, final Handler callback) {
		new Thread() {
			public void run() {
				Process process = null;
				OutputStream out = null;
				InputStream in = null;
				try {
					// 请求root
					process = Runtime.getRuntime().exec("su");
					if(callback!=null){
						Message msg = new Message();
						msg.what = msgwhat;
						msg.obj = 0; //卸载开始
						callback.sendMessage(msg);
					}
					SLog.e("test", "111");
					out = process.getOutputStream();
					SLog.e("test", "222");
					// 调用卸载
					out.write(("pm uninstall " + pkg + "\n").getBytes());
					SLog.e("test", "333");
					in = process.getInputStream();
					SLog.e("test", "444");
					byte[] bs = new byte[256];
					int len = in.read(bs);
					SLog.e("test", "555 len = "+len);
					if(-1 != len) {
						//取得root成功 
						String state = new String(bs, 0, len);
						SLog.e("test", "111 state = "+state);
						if (state.equals("Success\n")) {
							if(callback!=null){
								Message msg = new Message();
								msg.what = msgwhat;
								msg.obj = 1; //卸载成功
								callback.sendMessage(msg);
							}
						}else {
							if(callback!=null){
								Message msg = new Message();
								msg.what = msgwhat;
								msg.obj = 2; //卸载失败
								callback.sendMessage(msg);
							}
						}
					}else {
						//取得root失败
						uninstall(activity,pkg);
					}
					
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (out != null) {
							out.flush();
							out.close();
						}
						if (in != null) {
							in.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值