【发布时间】:2011-07-12 21:27:35
【问题描述】:
我正在开发一个 android 应用程序,我需要 android 设备功能。我知道,通过使用包管理器,getSystemAvailableFeatures 方法应该可用。该方法仍然不可用,任何人都可以通过发布一些与之相关的示例或源代码来帮助我。
【问题讨论】:
-
你尝试
getSystemAvailableFeatures()时发生了什么??
我正在开发一个 android 应用程序,我需要 android 设备功能。我知道,通过使用包管理器,getSystemAvailableFeatures 方法应该可用。该方法仍然不可用,任何人都可以通过发布一些与之相关的示例或源代码来帮助我。
【问题讨论】:
getSystemAvailableFeatures()时发生了什么??
我使用以下函数来确定某个功能是否可用:
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)) {
...
}
【讨论】:
<uses-feature> 标记来宣传您的应用程序依赖于支持特定功能/功能。
如果您知道要检查的功能,则无需列举所有系统功能并对照您要查找的功能进行检查。从 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");
【讨论】: