美文网首页
获取Android手机中安装的所有应用的基本信息

获取Android手机中安装的所有应用的基本信息

作者: IBegins | 来源:发表于2017-09-04 17:26 被阅读0次

获取Android手机中安装的所有应用的基本信息

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class PackageUtils {
    private Context context;

    public PackageUtils(Context context) {
        this.context = context;
    }

    public List<PackageInfo> getPackageInfos() {
        PackageManager packageManager = context.getPackageManager();
        return packageManager.getInstalledPackages(0);
    }

    public AllPackageInfos getAllPackageInfos() {
        PackageManager packageManager = context.getPackageManager();
        AllPackageInfos infos = new AllPackageInfos();
        for (PackageInfo info : getPackageInfos()) {
            AllPackageInfo aInfo = new AllPackageInfo();
            aInfo.setVersionName(info.versionName);
            aInfo.setVersionCode(info.versionCode);
            aInfo.setPackageName(info.packageName);
            aInfo.setFirstInstallTime(info.firstInstallTime);
            aInfo.setLastUpdateTime(info.lastUpdateTime);
            aInfo.setName(packageManager.getApplicationLabel(info.applicationInfo).toString());
            if ((ApplicationInfo.FLAG_SYSTEM & info.applicationInfo.flags) != 0) {
                aInfo.setSystem(true);
            }
            infos.add(aInfo);
        }
        return infos;
    }

    public String getAllPackageInfo() {
        Gson gson = new Gson();
        try {
            Type founderListType = new TypeToken<AllPackageInfos>() {}.getType();
            String infoJson = gson.toJson(getAllPackageInfos(), founderListType);
            return infoJson;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(long time) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = new Date(time);
        res = simpleDateFormat.format(date);
        return res;
    }
    class AllPackageInfos {
        private List<AllPackageInfo> packageInfos;

        public AllPackageInfos() {
            this.packageInfos = new ArrayList<>();
        }

        public List<AllPackageInfo> getInfos() {
            return packageInfos;
        }

        public void add(AllPackageInfo allPackageInfo) {
            packageInfos.add(allPackageInfo);
        }
    }

    class AllPackageInfo {
        /**
         * 版本名称
         */
        private String versionName;
        /**
         * 应用名称
         */
        private String name;
        /**
         * 版本号
         */
        private int versionCode;
        /**
         * 包名
         */
        private String packageName;
        /**
         * 是否是系统应用
         */
        private boolean system;
        /**
         * 第一次安装时间
         */
        private String firstInstallTime;
        /**
         * 最后更新时间
         */
        private String lastUpdateTime;

        public boolean isSystem() {
            return system;
        }

        public void setSystem(boolean system) {
            this.system = system;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        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 getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }

        public String getFirstInstallTime() {
            return firstInstallTime;
        }

        public void setFirstInstallTime(long firstInstallTime) {
            this.firstInstallTime = stampToDate(firstInstallTime);
        }

        public String getLastUpdateTime() {
            return lastUpdateTime;
        }

        public void setLastUpdateTime(long lastUpdateTime) {
            this.lastUpdateTime = stampToDate(lastUpdateTime);
        }
    }
}

相关文章

网友评论

      本文标题:获取Android手机中安装的所有应用的基本信息

      本文链接:https://www.haomeiwen.com/subject/djiejxtx.html