如何使用包管理器获取Android设备功能

发布于 2024-10-21 09:58:28 字数 143 浏览 7 评论 0原文

我正在开发一个 Android 应用程序,我需要 Android 设备功能。我知道,通过使用包管理器, getSystemAvailableFeatures 方法应该可用。该方法仍然不可用,任何人都可以通过发布一些与此相关的示例或源代码来帮助我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-10-28 09:58:28

我使用以下函数来确定某个功能是否可用:

    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;
    }

用法(即来自 Activity 类):

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

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)) {
        ...
    }
金橙橙 2024-10-28 09:58:28

如果您知道要检查的功能,则无需枚举所有系统功能并对照您要查找的功能进行检查。从 API 级别 5 开始,您可以使用 PackageManager.hasSystemFeature() 函数执行与上一个答案中所示的 isFeatureAvailable() 函数相同的工作。

例如...

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文