在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Android 中静默安装实现

2016-12-20 13:16| 发布者: zhangjf| 查看: 473| 评论: 0

摘要: 静默安装原理: 1.需要获取root的操作权限 2.通过命令式的方式直接进行安装APK。在使用 Android Studio debug安装的时候可以看到控制台上的命令 import android.content.Context; import android.co ...

静默安装原理:

1.需要获取root的操作权限

2.通过命令式的方式直接进行安装APK。在使用 Android Studio debug安装的时候可以看到控制台上的命令
  1. import android.content.Context;
  2. import android.content.Intent;
  3. import android.content.pm.PackageManager;
  4. import android.net.Uri;
  5. import android.util.Log;
  6. import java.io.DataOutputStream;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. /**
  11. * <p>名称:com.singno.VersionManager</p>
  12. * <p>描述:</p>
  13. * <pre>
  14. * APK版本管理器
  15. * 版本检查,版本更新等
  16. * </pre>
  17. *
  18. * @author 鲍建明
  19. * @version 2.1.0
  20. * @date 2015/4/30/16:28
  21. */
  22. public class VersionManager {
  23. private static final String TAG = VersionManager.class.getName();
  24. private Context context;
  25. public VersionManager(Context context){
  26. this.context = context;
  27. }
  28. /**
  29. * 检查版本号是否相同
  30. * @param versionCode
  31. * @return
  32. */
  33. public boolean isSameVersion(int versionCode){
  34. return getCurrentVersion() != versionCode ? Boolean.FALSE : Boolean.TRUE;
  35. }
  36. /**
  37. * 静默安装,安装之前必须要获取到ROOT权限
  38. * 原理:1.先获取到ROOT权限
  39. * 2.在通过命令的方式直接安装APK
  40. * @return
  41. */
  42. public boolean silenceInstall(File file){
  43. Process process = null;
  44. OutputStream out = null;
  45. DataOutputStream dataOutputStream = null;
  46. try {
  47. process = Runtime.getRuntime().exec("su");
  48. out = process.getOutputStream();
  49. dataOutputStream = new DataOutputStream(out);
  50. dataOutputStream.writeBytes("chmod 777 " + file.getPath() + "n");
  51. dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " + file.getPath());
  52. // 提交命令
  53. dataOutputStream.flush();
  54. int value = process.waitFor();
  55. if( value == 0){
  56. return Boolean.TRUE;
  57. }
  58. return Boolean.FALSE;
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. return Boolean.FALSE;
  62. }finally{
  63. try {
  64. if( dataOutputStream != null ){
  65. dataOutputStream.close();
  66. }
  67. if( out != null ){
  68. out.close();
  69. }
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. /**
  76. * 普通的安装应用方式
  77. * @param file 安装包文件
  78. */
  79. public void installApk(File file){
  80. Intent i = new Intent(Intent.ACTION_VIEW);
  81. i.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
  82. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  83. this.context.startActivity(i);
  84. }
  85. /**
  86. * 获取服务端中的版本号
  87. * 这个自行完成
  88. * @return
  89. */
  90. public int getHttpVersion(){
  91. return 0;
  92. }
  93. /**
  94. * 获取当前APK的版本号
  95. * @return 当前APK的版本号
  96. */
  97. public int getCurrentVersion(){
  98. try {
  99. return this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0).versionCode;
  100. } catch (PackageManager.NameNotFoundException e) {
  101. e.printStackTrace();
  102. Log.e(TAG, "获取版本号失败");
  103. return 0;
  104. }
  105. }
  106. /**
  107. * 下载APK
  108. */
  109. public void downApk(){
  110. new Thread(new DownApk()).start();
  111. }
  112. /**
  113. * 显示下载进度提示框
  114. */
  115. private void showDownloadDialog(){
  116. }
  117. /**
  118. * 显示软件更新提示对话框
  119. */
  120. private void showNoticeDialog(){
  121. }
  122. /**
  123. * 下载APk的类
  124. */
  125. class DownApk implements Runnable{
  126. @Override
  127. public void run() {
  128. }
  129. }
  130. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 11:40

Copyright 2015-2025 djqfx

返回顶部