获取手机应用列表和APP应用信息

各种获取应用信息小功能,做个笔记:

  /**
     * 获取版本名
     *
     * @param context
     * @return 获取版本名
     */
    public static String getVersionName(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;

    }
   /**
     * 获取版本号
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
/**
     * 获取application中指定的meta-data
     *
     * @return 如果没有获取成功(没有对应值, 或者异常),则返回值为空
     */
    public static String getAppMetaData(Context ctx, String key) {
        if (ctx == null || TextUtils.isEmpty(key)) {
            return null;
        }
        String resultData = "";
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        resultData = applicationInfo.metaData.getString(key);
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return resultData;
    }
 /**
     * 获取安装本应用的手机及手机卡信息
     *
     * @param context
     * @return
     */
    public static String getPhoneInfo(Context context) {
        /**
         * imei,移动设备标识码
         */
        String device_id = "";
        /**
         *手机号码标识
         */
        String imsi = "";
        /**
         * 手机型号
         */
        String phoneModel = "";
        /**
         * 手机操作系统版本
         */
        String phoneVersion = "";
        /**
         * 手机号码
         */
        String phoneNum = "";
        /**
         * 手机物理标识
         */
        String mac = "";

        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                device_id = tm.getDeviceId();
                imsi = tm.getSubscriberId();
                phoneNum = tm.getLine1Number();
                phoneModel = android.os.Build.MODEL;
                phoneVersion = android.os.Build.VERSION.RELEASE;
            }
            android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            mac = wifi.getConnectionInfo().getMacAddress();

            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty(device_id)) {//如果Android pad没有IMEI
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                        android.provider.Settings.Secure.ANDROID_ID);
            }

            json.put("mac", mac);
            json.put("imei", device_id);
            json.put("imsi", imsi);
            json.put("phoneModel", phoneModel);
            json.put("phoneVersion", phoneVersion);
            json.put("phoneNum", phoneNum);

            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean checkPermission(Context context, String permission) {
        boolean result = false;
        if (Build.VERSION.SDK_INT >= 23) {
            if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        } else {
            PackageManager pm = context.getPackageManager();
            if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        }
        return result;
    }

获取手机上安装的所有应用:

/**
 * 获取手机应用程序
 * Created by songyaru on 16/9/29.
 */
public class AppInfoService {
    private Context context;
    private PackageManager pm;

    public AppInfoService(Context context) {
        this.context = context;
        pm = context.getPackageManager();
    }

    /**
     * 得到手机中所有的应用程序信息
     *
     * @return
     */
    public List<AppInfo> getAppInfos() {
        //创建要返回的集合对象
        List<AppInfo> appInfos = new ArrayList();
        //获取手机中所有安装的应用集合
        List<ApplicationInfo> applicationInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        //遍历所有的应用集合
        for (ApplicationInfo info : applicationInfos) {

            AppInfo appInfo = new AppInfo();

            //获取应用程序的图标
            Drawable app_icon = info.loadIcon(pm);
            appInfo.setApp_icon(app_icon);

            //获取应用的名称
            String app_name = info.loadLabel(pm).toString();
            appInfo.setApp_name(app_name);

            //获取应用的包名
            String packageName = info.packageName;
            appInfo.setPackagename(packageName);
            try {
                //获取应用的版本号
//                PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
                PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES
                        | PackageManager.GET_PERMISSIONS);
                String app_version = packageInfo.versionName;
                appInfo.setApp_version(app_version);

                //获取应用的使用权限
                String[] app_permission = packageInfo.requestedPermissions;
                Log.e("appinfo", app_permission.length + "=数量");
                appInfo.setAppPremission(app_permission);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

            //判断应用程序是否是用户程序
            boolean isUserApp = filterApp(info);
            appInfo.setIsUserApp(isUserApp);


            appInfos.add(appInfo);
        }
        return appInfos;
    }

    //判断应用程序是否是用户程序
    public boolean filterApp(ApplicationInfo info) {
        //原来是系统应用,用户手动升级
        if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
            return true;
            //用户自己安装的应用程序
        } else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            return true;
        }
        return false;
    }
}

用与存储应用信息的javaBean:

/**
 * Created by songyaru on 16/9/29.
 * 获取的应用基本信息实体类
 */
public class AppInfo {
    //图标
    private Drawable app_icon;
    //应用名称
    private String app_name;
    //应用版本号
    private String app_version;
    //应用包名
    private String packagename;
    //是否是用户app
    private boolean isUserApp;
    //应用所需权限
    private String[] appPremission;


    public AppInfo() {
        super();
    }

    public AppInfo(Drawable app_icon, String app_name, String app_version,
                   String packagename) {
        super();
        this.app_icon = app_icon;
        this.app_name = app_name;
        this.app_version = app_version;
        this.packagename = packagename;
    }


    public AppInfo(Drawable app_icon, String app_name, String app_version,
                   String packagename, boolean isUserApp, String[] appPremission) {
        super();
        this.app_icon = app_icon;
        this.app_name = app_name;
        this.app_version = app_version;
        this.packagename = packagename;
        this.isUserApp = isUserApp;
        this.appPremission = appPremission;
    }

    public Drawable getApp_icon() {
        return app_icon;
    }

    public void setApp_icon(Drawable app_icon) {
        this.app_icon = app_icon;
    }

    public String getApp_name() {
        return app_name;
    }

    public void setApp_name(String app_name) {
        this.app_name = app_name;
    }

    public String getApp_version() {
        return app_version;
    }

    public void setApp_version(String app_version) {
        this.app_version = app_version;
    }

    public String getPackagename() {
        return packagename;
    }

    public void setPackagename(String packagename) {
        this.packagename = packagename;
    }

    public boolean isUserApp() {
        return isUserApp;
    }

    public void setIsUserApp(boolean isUserApp) {
        this.isUserApp = isUserApp;
    }

    public String[] getAppPremission() {
        return appPremission;
    }

    public void setAppPremission(String[] appPremission) {
        this.appPremission = appPremission;
    }

    @Override
    public String toString() {
        return "AppInfo{" +
                "app_icon=" + app_icon +
                ", app_name='" + app_name + '\'' +
                ", app_version='" + app_version + '\'' +
                ", packagename='" + packagename + '\'' +
                ", isUserApp=" + isUserApp +
                ", appPremission=" + Arrays.toString(appPremission) +
                '}';
    }
}
参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

mango不想早起

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值