简体   繁体   English

从较新的API级别中删除的旧API级别的使用类

[英]Use class from older API level removed in newer API level

I want to access the android.webkit.WebSettingsClassic class on API level 16 but compile my application for API level 19. The WebSettingsClassic class has been removed in API level 19 and thus the compiler cannot see it. 我想访问API级别16上的android.webkit.WebSettingsClassic类,但将我的应用程序编译为API级别WebSettingsClassic类已在API级别19中删除,因此编译器无法看到它。 How do I solve this? 我该如何解决?

I solved it using the following modified code. 我使用以下修改的代码解决了它。 This allows you to compile against API level 19. 这使您可以根据API级别19进行编译。

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String getUserAgent(final Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return WebSettings.getDefaultUserAgent(context);
    }
    else {
        try {
            final Class<?> webSettingsClassicClass = Class.forName("android.webkit.WebSettingsClassic");
            final Constructor<?> constructor = webSettingsClassicClass.getDeclaredConstructor(Context.class, Class.forName("android.webkit.WebViewClassic"));
            constructor.setAccessible(true);
            final Method method = webSettingsClassicClass.getMethod("getUserAgentString");
            return (String) method.invoke(constructor.newInstance(context, null));
        }
        catch (final Exception e) {
            return new WebView(context).getSettings()
                    .getUserAgentString();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在API级别12中添加UsbAccessory类而不是API级别8? - Why UsbAccessory class is added in API Level 12 not from API level 8? 较新版本的android API级别4和更高版本上的Filenotfoundexception - Filenotfoundexception on newer versions of android API Level 4 and later 验证使用较新 JDK 构建的库的 API 级别的向后兼容性 - Verify backward-compatibility on API level of a library built with a newer JDK 我可以在旧版本的Elasticsearch上使用新版本的Java高级别Rest客户端吗? - Can I use newer version of java high level rest client on an older version of elasticsearch? 如何在可能的情况下使用 API 8 级 class,否则什么也不做 - How to use an API level 8 class when possible, and do nothing otherwise 低于api级别11设备的FragmentActivity类 - FragmentActivity class for below api level 11 devices 在API级别14之前使用钥匙串 - Use Keychain before api level 14 在带有外部库的android中使用更高的API级别 - Use higher API level in android with an external library TimeZone.observesDaylightTime() 需要 API 级别 24; 是否可以在较旧的 API 版本中获得它? - TimeZone.observesDaylightTime() requires API level 24; is it possible to get it in older API versions? api级别8 HttpURLConnection - api level 8 HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM