android 实现静默安装和卸载
来源:互联网 发布:游戏软件编程 编辑:程序博客网 时间:2024/05/22 07:04
通过获取系统root权限调用pm install -r apkpath 和pm uninstall pkgname 来实现.
附上代码:
// install apkprivate 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 apkprivate 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);}// installpublic 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 {// 请求rootprocess = 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 {// 请求rootprocess = 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();}
- android实现静默卸载和静默安装
- android 实现静默安装和卸载
- android 实现静默安装和卸载
- Android静默安装和静默卸载
- Android--静默安装和静默卸载
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- android 静默安装、卸载实现
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- android 静默安装和卸载
- android静默安装和卸载
- 西电1232 求斐波拉契数列的后四位
- 为什么丧失了“慢”的能力?
- 正则表达式之概述、测试工具
- HDU 1003 Max Sum - 简单动态规划
- web开发总结
- android 实现静默安装和卸载
- flex 映射DataGrid,修改DataGridColumn显示值
- linux ssh命令笔记
- 轻松实现Windows Server 2003 开机自动登录
- 六、把文件存放在SDCard
- android(二)
- 常用的小技巧
- Azkaban文档
- Linux下高并发socket最大连接数