android 实现静默安装和卸载
2013-03-25 10:18
267 查看
通过获取系统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(); }
相关文章推荐
- android 代码实现软件的静默安装,卸载功能(转载)
- android 实现静默安装、卸载
- 转载 :Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- android 实现静默安装、卸载
- android 实现静默安装、卸载
- Android实现静默安装与卸载
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- android 实现静默安装、卸载
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- android 实现静默安装、卸载
- Android静默方式实现批量安装卸载应用程序
- 【Android应用开发】-(18)静默方式实现批量安装卸载应用程序
- android 实现静默安装、卸载
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- Android实现静默安装与卸载
- android 实现静默安装、卸载
- android 实现静默安装、卸载(图)
- Android静默方式实现批量安装卸载应用程序
- android 实现静默安装和卸载
- Android静默方式实现批量安装卸载APK