android - API < 11 上的 getWindow().hasFeature()

标签 android android-actionbar-compat

我需要检查是否已在 ActionBarCompat 实例上设置叠加功能。 getWindow().hasFeature() 方法仅适用于 API 11 及更高版本。

如何检查 API < 11 上的功能?

编辑:根据评论,getFeatures 方法应该可以从 API 1 获得,但它是 protected 范围,我需要从另一个类访问该功能。另一方面,我需要使用的 hasFeature 方法仅适用于 API 11 及更高版本。这是 Android Studio 向我显示的内容,应用程序在 2.3.3 设备上崩溃。

Image from AS

仅供引用,此处使用的 Activity 类是从 ActionBarCompat 库扩展 ActionBarActivity 的自定义类。不知道这是否应该有所作为。

最佳答案

您可以使用 The Reflection API 访问私有(private) 方法.

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

关于android - API < 11 上的 getWindow().hasFeature(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925708/

相关文章:

android - 如何使用抽屉导航添加自定义操作栏?

android - appcompat-v7 工具栏菜单 - 选择颜色不影响文本背景

java - 在android中测试, fragment 上的NullPointerException

android - Context.startForegroundService() 仅在 Android 11 Samsung 设备上没有调用 Service.startForeground()

android - 如何在 Android <selector> 中使用 VectorDrawable

Android:图标不会出现在操作栏中

android - ActionBarCompat 下拉菜单上的样式单选按钮

android - 无法使用支持库获取 ActionBar MenuItem 的操作 View

Android:滑动菜单仅滑动内容

Android 录音图表/图表