How to get Android device Features using package manager

北战南征 提交于 2019-11-30 04:15:30
GrAnd

I use the following function to determine if a feature is available:

    public final static boolean isFeatureAvailable(Context context, String feature) {
        final PackageManager packageManager = context.getPackageManager();
        final FeatureInfo[] featuresList = packageManager.getSystemAvailableFeatures();
        for (FeatureInfo f : featuresList) {
            if (f.name != null && f.name.equals(feature)) {
                 return true;
            }
        }

       return false;
    }

The usage (i.e from Activity class):

    if (isFeatureAvailable(this, PackageManager.FEATURE_CAMERA)) {
        ...
    }
Tim Goss

If you know the feature you want to check then you don't need to enumerate all system features and check against the one you're looking for. Since API level 5 you can use the PackageManager.hasSystemFeature() function to do the same job as the isFeatureAvailable() function shown in the previous answer.

For example...

PackageManager packageManager = this.getPackageManager();

if (packageManager.hasSystemFeature(PackageManager.FEATURE_NFC))
    Log.d("TEST", "NFC IS AVAILABLE\n");
else
    Log.d("TEST", "NFC IS *NOT* AVAILABLE\n");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!