How to get Android device Features using package manager

微笑、不失礼 提交于 2019-12-30 00:40:24

问题


I am developing an android application and I need android device features. I know that, by using package manager, getSystemAvailableFeatures method should be available. Still the method is not available Can any one help me by post some example or source code related to that.


回答1:


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)) {
        ...
    }



回答2:


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");


来源:https://stackoverflow.com/questions/5263068/how-to-get-android-device-features-using-package-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!