The following is a tool class. It uses recursion to scan the APK file on the SD card to determine whether the application has been installed on the mobile phone. If so, it determines whether the application can be upgraded. DetailsCodeAs follows:
/*** Obtain the APK File Information class on the mobile phone, mainly to determine whether or not to install it on the mobile phone, the installed version compares the existing APK version information * <a href = "http://my.oschina.net/arthor" rel = nofollow target = _ blank> @ author </a> Dylan */public class apksearchutils {Private Static int installed = 0; // It indicates that the APK file has been installed, and the current version is Private Static int uninstalled = 1; // It indicates that Private Static int installed_update = 2 is not installed; // It indicates that the APK file has been installed, the version is lower than the current version. You can click the button to update private context; private list <myfile> myfiles = new Arraylist <myfile> (); public list <myfile> getmyfiles () {return myfiles;} public void setmyfiles (list <myfile> myfiles) {This. myfiles = myfiles;} public apksearchutils (context) {super (); this. context = context;}/*** @ Param ARGs * uses recursive ideas to recursively find the APK file under each directory */Public void findallapkfile (File file) {// file on the mobile phone. Currently, only the APK file on the SD card is determined. // file = environment. getdatadirectory (); // If (file. isfile () {Str Ing name_s = file. getname (); myfile = new myfile (); string apk_path = NULL; // mimetypemap. getsingleton () if (name_s.tolowercase (). endswith (". APK ") {apk_path = file. getabsolutepath (); // absolute path strength of the APK file // system. out. println ("----" + file. getabsolutepath () + "" + // name_s); packagemanager PM = context. getpackagemanager (); packageinfo = PM. getpackagearchiveinfo (apk_path, packagemanager. get_a Ctivities); applicationinfo appinfo = packageinfo. applicationinfo;/** get the APK icon */appinfo. sourcedir = apk_path; appinfo. publicsourcedir = apk_path; drawable apk_icon = appinfo. loadicon (PM); myfile. setapk_icon (apk_icon);/** get package name */string packagename = packageinfo. packagename; myfile. setpackagename (packagename);/** absolute path strength of APK */myfile. setfilepath (file. getabsolutepath ();/** APK version name: string */string versionn Ame = packageinfo. versionname; myfile. setversionname (versionname);/** APK version number int */INT versioncode = packageinfo. versioncode; myfile. setversioncode (versioncode);/** installation and processing type */INT type = dotype (PM, packagename, versioncode); myfile. setinstalled (type); log. I ("OK", "processing type:" + String. valueof (type) + "\ n" + "------------------ I am a pure split line -------------------"); myfiles. add (myfile);} // string apk_app = name_s.substring (Name_s.lastindexof (".");} else {file [] files = file. listfiles (); If (files! = NULL & files. length> 0) {for (File file_str: Files) {findallapkfile (file_str) ;}}}/** determine whether the application has been installed on your phone, the following cases may occur: * 1. not Installed. In this case, click "Install" to install ** 2. installed. You can uninstall the application by clicking "installed". * 3. installed, but the version is updated, click "Update" to install the application. * // *** determine the installation status of the application on your mobile phone. * @ Param PM packagemanager * @ Param packagename determine the package name of the application * @ Param versioncode determine the application version number */private int dotype (packagemanager pm, string packagename, int versioncode) {list <packageinfo> pakageinfos = PM. getinstalledpackages (packagemanager. get_uninstalled_packages); For (packageinfo Pi: pakageinfos) {string pi_packagename = pi. packagename; int pi_versioncode = pi. versioncode; // if the package name exists in an application that has been installed by the system, if (packagename. endswith (pi_packagename) {// log. I ("test", "This application has been installed"); If (versioncode = pi_versioncode) {log. I ("test", "installed, You can uninstall this application without updating"); Return installed;} else if (versioncode> pi_versioncode) {log. I ("test", "installed, updated"); Return installed_update ;}} log. I ("test", "this application is not installed, you can install it"); Return uninstalled ;}}