批量安裝於卸載

有嘗試過在Android手機裏批量安裝於批量卸載你的應用程序嗎,現在就簡單教你怎麼做,其實很簡單,還有後檯安裝於卸載問題:

1.後臺安裝於卸載步驟:

首先你需要使用其Pm命令,如一下

  1. pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH  
  2. pm uninstall [-k] PACKAGE 
代碼實現也很簡單,使用流的方式來進行相關操作:

  1. public static void execCommand(String... command) {  
  2. 02      Process process = null;  
  3. 03      try {  
  4. 04          process = new ProcessBuilder().command(command).start();  
  5. 05          //對於命令的執行結果我們可以通過流來讀取  
  6. 06          // InputStream in = process.getInputStream();  
  7. 07          // OutputStream out = process.getOutputStream();  
  8. 08          // InputStream err = process.getErrorStream();  
  9. 09      } catch (IOException e) {  
  10. 10          e.printStackTrace();  
  11. 11      } finally {  
  12. 12          if (process != null)  
  13. 13              process.destroy();  
  14. 14      }  
  15. 15  }  
  16.    
  17. 1   execCommand("pm""install""-f", filePath);//安裝apk,filePath爲apk文件路徑,如/mnt/sdcard/ApiDemos.apk  
  18. 2   execCommand("pm""uninstall", packageName);//卸載apk,packageName爲包名,如com.example.android.apis 

注意一下:

  1. 編譯生成apk時,要在你的manifest文件下添加android:sharedUserId="android.uid.system",編譯完成之後還無法正常安裝,會出現Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE錯誤,此時,要爲apk重新簽名。  
  2.   
  3. 在android源碼\build\target\product\security中找platform.pk8和platform.x509.pem兩個文件,在android 編繹目錄out下找到 signapk.jar 這個包(源碼目錄\build\tools\signapk),並把編譯好的apk(如PMDemo.apk)放在同一目錄下,在重新簽名之前,用rar文件打開apk文件,進入META-INF目錄下,將CERT.SF和CERT.RSA這兩個文件刪除掉,然後在命令行中執行以下命令:  
  4.    
  5. 1   java -jar signapk.jar platform.x509.pem platform.pk8 PMDemo.apk NewPMDemo.apk  
  6. 安裝前先把舊的apk卸載,這樣重新簽名之後的apk就可以正常安裝了。   

很簡單吧,下面看下批量操作:

直接看代碼吧:

  1. 通常情況下,android是沒有提供靜默方式的上層接口,我們需要在android源代碼下來調用這個隱藏的接口來完成靜默安裝。  
  2. 最重要的就是參考android系統目錄下的packages/apps/PackageInstaller,  
  3. 當中有兩個文件 PackageInstallerActivity.java,InstallAppProgress.java ,前者就是我們通常看到的帶有提示對話框的安裝應用程序,後者是點確定安裝後調用的intent。  
  4. 現提供一個靜默安裝的關鍵類,該類在android2.2下成功編譯, 其中通過循環調用接口instatllBatch則可實現批量安裝  
  5. 當然最後的應用程序別忘記添加權限  
  6.   
  7.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  8.         <uses-permission android:name="android.permission.INSTALL_PACKAGES" />  
  9.         <uses-permission android:name="android.permission.DELETE_PACKAGES" />  
  10.         <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />  
  11.         <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  12.         <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />  
  13.   
  14. package com.android.util;  
  15.   
  16. import java.io.File;    
  17.   
  18. import java.io.FileNotFoundException;    
  19.   
  20. import java.io.FileOutputStream;    
  21.   
  22. import java.io.IOException;    
  23.   
  24. import android.content.Context;    
  25.   
  26. import android.content.Intent;    
  27.   
  28. import android.content.pm.PackageInfo;    
  29.   
  30. import android.content.pm.PackageManager;    
  31.   
  32. import android.content.pm.PackageManager.NameNotFoundException;    
  33.   
  34. import android.content.pm.ApplicationInfo;  
  35.   
  36. import android.content.pm.PackageParser;  
  37.   
  38. import android.net.Uri;    
  39.   
  40. import android.util.Log;    
  41.   
  42. import android.util.DisplayMetrics;  
  43.   
  44. import android.content.pm.IPackageInstallObserver;    
  45.   
  46. import android.content.pm.IPackageDeleteObserver;    
  47.   
  48. import android.os.FileUtils;    
  49.   
  50. import android.os.Handler;  
  51.   
  52. import android.os.Message;  
  53.   
  54.    
  55.   
  56. public class PackageInstaller {    
  57.   
  58.    
  59.   
  60. private File mTmpFile;    
  61.   
  62. private final int INSTALL_COMPLETE = 1;  
  63.   
  64. final static int SUCCEEDED = 1;  
  65.   
  66. final static int FAILED = 0;  
  67.   
  68. private final static String TAG = "PackInstaller";    
  69.   
  70. private Context mContext;    
  71.   
  72. private ApplicationInfo mAppInfo;  
  73.   
  74. public PackageInstaller(Context context) {    
  75.   
  76. mContext = context;    
  77.   
  78. }    
  79.   
  80. public void install(String path,String packageName){    
  81.   
  82. Intent intent = new Intent(Intent.ACTION_VIEW);    
  83.   
  84. intent.setDataAndType(Uri.fromFile(new File(path)),    
  85.   
  86. "application/vnd.android.package-archive");    
  87.   
  88. mContext.startActivity(intent);    
  89.   
  90. }    
  91.   
  92.    
  93.   
  94. public void instatllBatch(String path) {    
  95.   
  96. Log.i(TAG, "path=" + path);    
  97.   
  98. int installFlags = 0;    
  99.   
  100. Uri mPackageURI  = Uri.fromFile(new File(path));  
  101.   
  102. PackageParser.Package mPkgInfo = getPackageInfo(mPackageURI);  
  103.   
  104. mAppInfo =  mPkgInfo.applicationInfo;  
  105.   
  106. String packageName = mAppInfo.packageName;  
  107.   
  108. Log.i(TAG, "====install packageName ="+packageName);  
  109.   
  110. PackageManager pm = mContext.getPackageManager();    
  111.   
  112. try {    
  113.   
  114. PackageInfo pi = pm.getPackageInfo(packageName,    
  115.   
  116. PackageManager.GET_UNINSTALLED_PACKAGES);    
  117.   
  118. if (pi != null) {    
  119.   
  120. installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;    
  121.   
  122. }    
  123.   
  124. catch (NameNotFoundException e) {    
  125.   
  126. }    
  127.   
  128. if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) {    
  129.   
  130. Log.w(TAG, "Replacing package:" + packageName);    
  131.   
  132. }    
  133.   
  134.    
  135.   
  136. PackageInstallObserver observer = new PackageInstallObserver();    
  137.   
  138. pm.installPackage(mPackageURI, observer, installFlags,    
  139.   
  140. packageName);    
  141.   
  142. }    
  143.   
  144. private class PackageInstallObserver extends IPackageInstallObserver.Stub {    
  145.   
  146. public void packageInstalled(String packageName, int returnCode) {    
  147.   
  148. // Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);    
  149.   
  150. // msg.arg1 = returnCode;    
  151.   
  152. // mHandler.sendMessage(msg);    
  153.   
  154. Log.i(TAG, "====INSTALL_COMPLETE");    
  155.   
  156. }    
  157.   
  158. }    
  159.   
  160. private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {    
  161.   
  162. public void packageDeleted(boolean succeeded) {    
  163.   
  164. //            Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);    
  165.   
  166. //            msg.arg1 = succeeded?SUCCEEDED:FAILED;    
  167.   
  168. //            mHandler.sendMessage(msg);    
  169.   
  170. Log.i(TAG, "====UNINSTALL_COMPLETE");    
  171.   
  172. }    
  173.   
  174. }    
  175.   
  176.     public void uninstall(String packageName){    
  177.   
  178. Uri packageURI = Uri.parse("package:" + packageName);    
  179.   
  180. Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,    
  181.   
  182. packageURI);    
  183.   
  184. mContext.startActivity(uninstallIntent);    
  185.   
  186. }    
  187.   
  188.    
  189.   
  190. public void uninstallBatch(String packageName) {    
  191.   
  192. PackageDeleteObserver observer = new PackageDeleteObserver();    
  193.   
  194. mContext.getPackageManager().deletePackage(packageName, observer, 0);    
  195.   
  196.    
  197.   
  198. }    
  199.   
  200. public  PackageParser.Package getPackageInfo(Uri packageURI) {  
  201.   
  202. final String archiveFilePath = packageURI.getPath();  
  203.   
  204. PackageParser packageParser = new PackageParser(archiveFilePath);  
  205.   
  206. File sourceFile = new File(archiveFilePath);  
  207.   
  208. DisplayMetrics metrics = new DisplayMetrics();  
  209.   
  210. metrics.setToDefaults();  
  211.   
  212. PackageParser.Package pkg =  packageParser.parsePackage(sourceFile,  
  213.   
  214. archiveFilePath, metrics, 0);   
  215.   
  216. // Nuke the parser reference.  
  217.   
  218. packageParser = null;  
  219.   
  220. return pkg;  
  221.   
  222. }  
  223.   
  224. public   ApplicationInfo getApplicationInfo(Uri packageURI) {  
  225.   
  226. final String archiveFilePath = packageURI.getPath();  
  227.   
  228. PackageParser packageParser = new PackageParser(archiveFilePath);  
  229.   
  230. File sourceFile = new File(archiveFilePath);  
  231.   
  232. DisplayMetrics metrics = new DisplayMetrics();  
  233.   
  234. metrics.setToDefaults();  
  235.   
  236. PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0);  
  237.   
  238. if (pkg == null) {  
  239.   
  240. return null;  
  241.   
  242. }  
  243.   
  244. return pkg.applicationInfo;  
  245.   
  246. }  
  247.   
  248. private Handler mHandler = new Handler() {  
  249.   
  250. public void handleMessage(Message msg) {  
  251.   
  252. switch (msg.what) {  
  253.   
  254. case INSTALL_COMPLETE:  
  255.   
  256. if(msg.arg1 == SUCCEEDED) {  
  257.   
  258.    
  259.   
  260. else {}  
  261.   
  262. break;  
  263.   
  264. default:  
  265.   
  266. break;  
  267.   
  268. }  
  269.   
  270. }  
  271.   
  272. };  
  273.   
  274. }  

 

發佈了81 篇原創文章 · 獲贊 65 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章