开发者

How to get Android device Features using package manager

开发者 https://www.devze.com 2023-02-16 14:34 出处:网络
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 availab

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 h开发者_Python百科elp me by post some example or source code related to that.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消