package yapplications; public class Applications { // private Drawable icon; // 存放图片 private String label; // 存放应用程序名 private String packageName; // 存放应用程序包名 private String versionName; private int versionCode; private String installTime; public String getInstallTime() { return installTime; } public void setInstallTime(String installTime) { this.installTime = installTime; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } public String getVersionName() { return versionName; } public void setVersionName(String versionName) { this.versionName = versionName; } public int getVersionCode() { return versionCode; } public void setVersionCode(int versionCode) { this.versionCode = versionCode; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } // public String getPackageName() { // return packageName; // } // // public void setPackageName(String packageName) { // this.packageName = packageName; // } }
package yapplications; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import ycore.DateFormatTools; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; public class ApplicationsHelper { public static String getApplicationsStr(Context context) { JSONObject applicationsJson=new JSONObject(); JSONArray arr=new JSONArray(); PackageManager pManager = context.getPackageManager(); List<PackageInfo> paklist = pManager.getInstalledPackages(0); ArrayList<Applications> appList = new ArrayList<Applications>(); if(paklist!=null) { for(int i=0;i<paklist.size();i++) { Applications app=new Applications(); PackageInfo pinfo = paklist.get(i); JSONObject obj=new JSONObject(); //set Icon // shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo)); //set Application Name String label=pinfo.applicationInfo.loadLabel(pManager).toString(); app.setLabel(label); String packageName=pinfo.packageName; app.setPackageName(packageName); String versionName=pinfo.versionName; app.setVersionName(versionName); int versionCode=pinfo.versionCode; app.setVersionCode(versionCode); long installTime=pinfo.firstInstallTime; String time=DateFormatTools.DateFormat(installTime); app.setInstallTime(time); try { obj.put("name", label); obj.put("packageName", packageName); obj.put("versionName", versionName); obj.put("versionCode", versionCode); obj.put("installTime",time); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } arr.put(obj); appList.add(app); } try { applicationsJson.put("applications", arr); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return applicationsJson.toString(); } return null; } // public static List<PackageInfo> getAllApps(Context context) { // List<PackageInfo> apps = new ArrayList<PackageInfo>(); // PackageManager pManager = context.getPackageManager(); // //获取手机内所有应用 // List<PackageInfo> paklist = pManager.getInstalledPackages(0); // for (int i = 0; i < paklist.size(); i++) { // PackageInfo pak = (PackageInfo) paklist.get(i); // //判断是否为非系统预装的应用程序 //// if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { //// // customs applications // apps.add(pak); //// } // } // return apps; // } }