代码改变世界

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

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

分类: Android 1762人阅读 评论(10) 收藏 举报

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

 

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

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。

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

posted on 2014-03-05 16:05  Captain林  阅读(502)  评论(0编辑  收藏  举报

导航