当前位置:   article > 正文

android 4.0.4系统下实现apk的静默安装和启动_tbrapkpath

tbrapkpath

       最近在android 4.0.4系统下实现apk的静默安装和启动的功能,这里和大家分享一下,希望能有所帮助。

源码如下:

  1. import java.io.DataOutputStream;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.pm.ActivityInfo;
  10. import android.content.pm.PackageInfo;
  11. import android.content.pm.PackageManager;
  12. import android.content.pm.ResolveInfo;
  13. public class InstallApkUtils {
  14. public static void installAndStartApk(final Context context, final String apkPath) {
  15. if ((apkPath==null) || (context==null)) {
  16. return;
  17. }
  18. File file = new File(apkPath);
  19. if (file.exists() == false) {
  20. return;
  21. }
  22. new Thread() {
  23. public void run() {
  24. String packageName = getUninstallApkPackageName(context, apkPath);
  25. if (silentInstall(apkPath)) {
  26. List<ResolveInfo> matches = findActivitiesForPackage(context, packageName);
  27. if ((matches!=null) && (matches.size()>0)) {
  28. ResolveInfo resolveInfo = matches.get(0);
  29. ActivityInfo activityInfo = resolveInfo.activityInfo;
  30. startApk(activityInfo.packageName, activityInfo.name);
  31. }
  32. }
  33. };
  34. }.start();
  35. }
  36. public static String getUninstallApkPackageName(Context context, String apkPath) {
  37. String packageName = null;
  38. if (apkPath == null) {
  39. return packageName;
  40. }
  41. PackageManager pm = context.getPackageManager();
  42. PackageInfo info = pm.getPackageArchiveInfo(apkPath,
  43. PackageManager.GET_ACTIVITIES);
  44. if (info == null) {
  45. return packageName;
  46. }
  47. packageName = info.packageName;
  48. return packageName;
  49. }
  50. public static List<ResolveInfo> findActivitiesForPackage(Context context, String packageName) {
  51. final PackageManager pm = context.getPackageManager();
  52. final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  53. mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  54. mainIntent.setPackage(packageName);
  55. final List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
  56. return apps != null ? apps : new ArrayList<ResolveInfo>();
  57. }
  58. public static boolean silentInstall(String apkPath) {
  59. String cmd1 = "chmod 777 " + apkPath + " \n";
  60. String cmd2 = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " + apkPath + " \n";
  61. return execWithSID(cmd1, cmd2);
  62. }
  63. private static boolean execWithSID(String... args) {
  64. boolean isSuccess = false;
  65. Process process = null;
  66. OutputStream out = null;
  67. try {
  68. process = Runtime.getRuntime().exec("su");
  69. out = process.getOutputStream();
  70. DataOutputStream dataOutputStream = new DataOutputStream(out);
  71. for (String tmp : args) {
  72. dataOutputStream.writeBytes(tmp);
  73. }
  74. dataOutputStream.flush(); // 提交命令
  75. dataOutputStream.close(); // 关闭流操作
  76. out.close();
  77. isSuccess = waitForProcess(process);
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. return isSuccess;
  82. }
  83. public static boolean startApk(String packageName, String activityName) {
  84. boolean isSuccess = false;
  85. String cmd = "am start -n " + packageName + "/" + activityName + " \n";
  86. try {
  87. Process process = Runtime.getRuntime().exec(cmd);
  88. isSuccess = waitForProcess(process);
  89. } catch (IOException e) {
  90. NLog.i(TAG, e.getMessage());
  91. e.printStackTrace();
  92. }
  93. return isSuccess;
  94. }
  95. private static boolean waitForProcess(Process p) {
  96. boolean isSuccess = false;
  97. int returnCode;
  98. try {
  99. returnCode = p.waitFor();
  100. switch (returnCode) {
  101. case 0:
  102. isSuccess = true;
  103. break;
  104. case 1:
  105. break;
  106. default:
  107. break;
  108. }
  109. } catch (InterruptedException e) {
  110. e.printStackTrace();
  111. }
  112. return isSuccess;
  113. }
  114. }

 

       如果要使用,还需以下步骤:

1、在AndroidManifest.xml文件里添加如下权限:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

2、进行系统签名。命令如下:

java -jar signapk.jar platform.x509.pem platform.pk8    XXX.apk    Signed_XXX.apk

备注:一般可在源码的目录\out\host\linux-x86\framework\下找到signapk.jar,在\build\target\product\security下找到签名文件platform.x509.pem和platform.pk8。

       好了,现在大功告成!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/675652
推荐阅读
相关标签
  

闽ICP备14008679号