Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Android device Features using package manager

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.

like image 940
Almand Avatar asked Mar 10 '11 16:03

Almand


2 Answers

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)) {
        ...
    }
like image 145
GrAnd Avatar answered Nov 14 '22 22:11

GrAnd


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");
like image 20
Tim Goss Avatar answered Nov 14 '22 23:11

Tim Goss