本文共 3466 字,大约阅读时间需要 11 分钟。
- package yapplications;
-
- public class Applications {
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
- }
- 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();
-
-
-
- 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) {
-
- e.printStackTrace();
- }
- arr.put(obj);
- appList.add(app);
- }
- try {
- applicationsJson.put("applications", arr);
- } catch (JSONException e) {
-
- e.printStackTrace();
- }
- return applicationsJson.toString();
- }
-
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
转载:http://blog.csdn.net/chaoyu168/article/details/49071529