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();}

原创粉丝点击