。getWindow()hasFeature()API上的LT&; 11 [英] getWindow().hasFeature() on API < 11
问题描述
我需要检查,如果叠加功能已经上 ActionBarCompat
实例设置。在 getWindow()。hasFeature()
方法仅适用于API 11及以上可用。
I need to check if the overlay feature has been set on an ActionBarCompat
instance. The getWindow().hasFeature()
method is only available on API 11 and up.
我如何检查API&LT的功能; 11?
How can I check the feature on API < 11 ?
编辑:基于注释,在 getFeatures
方法应可从API 1,但它是受保护的范围,我需要访问从另一个类的功能。在 hasFeature
方法,一个我需要使用,而另一方面是API 11及以上而已。这是Android的工作室展示了我和2.3.3设备上的应用程序崩溃。
Based on the comment, the getFeatures
method should be available from API 1 but it is protected scope and I need to access the feature from another class. The hasFeature
method, the one I need to use, on the other hand is API 11 and above only. This is what Android Studio shows me and the app crashes on a 2.3.3 device.
仅供参考,这里使用的活动类是从ActionBarCompat库扩展 ActionBarActivity
自定义类。不知道是不是应该有所作为。
FYI, the activity class used here is a custom class that extends ActionBarActivity
from the ActionBarCompat library. Don't know if that should make a difference.
推荐答案
您可以使用的私人方法=http://docs.oracle.com / JavaSE的/教程/体现/相对=nofollow>反射API 。
You can access private
methods using The Reflection API.
boolean hasFeature(int feature) {
Window window = getWindow(); //get the window instance.
if (android.os.Build.VERSION.SDK_INT >= 11) { // if we are running api level 11 and later
return window.hasFeature(feature); //call hasFeature
} else {
try {
Class c = window.getClass();
Method getFeatures = c.getDeclaredMethod("getFeatures");//get the getFeatures method using reflection
getFeatures.setAccessible(true);//make it public
Integer features = getFeatures.invoke(window, null); //invoke it
return (features.intValue() & (1 << feature)) != 0; //check if we have the feature and return the result.
} catch (Exception e) {
return false;//in case invocation fails with any reason
}
}
}
这篇关于。getWindow()hasFeature()API上的LT&; 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!