Android O 预览版:ConnectivityManager 删除的方法不向后兼容 [英] Android O Preview : ConnectivityManager removed methods are not backward compatible

查看:75
本文介绍了Android O 预览版:ConnectivityManager 删除的方法不向后兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先决条件和构建配置:我正在尝试通过为 Android O perview 设备设置以下构建配置参数来使用 AndroidStudio 2.4 review 5 构建我的应用程序,compileSdkVersion 'android-O'buildToolsVersion '25.0.0'compileOptions.encoding = 'ISO-8859-1'minSdk 版本 16targetSdkVersion 'O'//启用多索引支持.multiDexEnabled 真

Precondition and build config: I am trying to build my app with AndroidStudio 2.4 review 5 by setting below build config paramters for Android O perview device, compileSdkVersion 'android-O' buildToolsVersion '25.0.0' compileOptions.encoding = 'ISO-8859-1' minSdkVersion 16 targetSdkVersion 'O' // Enabling multidex support. multiDexEnabled true

问题描述:我的应用程序将以下这些已弃用的 ConnectivityManager 类方法用于具有旧版 android 的设备.-ConnectivityManager.startUsingNetworkFeature()- ConnectivityManager.stopUsingNetworkFeature()- ConnectivityManager.requestRouteToHost()

Issue Description: My app uses these below deprecated methods of ConnectivityManager class for devices with older version of android. -ConnectivityManager.startUsingNetworkFeature() - ConnectivityManager.stopUsingNetworkFeature() - ConnectivityManager.requestRouteToHost()

当我尝试构建具有上述 api 的应用程序时,出现如下编译时错误,

When I try to build my app which has these above api, it is giving compile time error as follows,

错误:(626, 48) 错误:找不到符号方法 startUsingNetworkFeature(int,String)错误:(7393, 27) 错误:找不到符号方法 stopUsingNetworkFeature(int,String)错误:(69, 36) 错误:找不到符号方法 requestRouteToHost(int,int)

Error:(626, 48) error: cannot find symbol method startUsingNetworkFeature(int,String) Error:(7393, 27) error: cannot find symbol method stopUsingNetworkFeature(int,String) Error:(69, 36) error: cannot find symbol method requestRouteToHost(int,int)

理想情况下,这些 api 不应出于向后兼容的目的而给出编译时错误.请告诉我如何解决这些编译时错误.

Ideally these api should not give compile time error for backward compatibity purpose. Please let me know how to resolve these compile time errors.

我无法从代码中删除这些方法,因为旧版 android 版本(低于 Android L 版本)的设备需要它们

I can't remove these methods from code as they are required for devices with older android version(less than Android L version)

推荐答案

因为我无法在 Antilope 的回答中添加评论,我会在这里更正.

Because I cannot add comments to Antilope's answer, I will correct it here.

应该是method.invoke(connectivityManager, ConnectivityManager.TYPE_MOBILE, "enableHIPRI");

所以一般代码看起来像:

So in general the code looks like:

try{
    java.lang.reflect.Method method = connectivityManager.getClass().getMethod("startUsingNetworkFeature",
    int.class, String.class); 
    resultInt = (int) method.invoke(connectivityManager, ConnectivityManager.TYPE_MOBILE, 
"enableHIPRI");
} catch (Exception e) { 
    //Do something 
}

try{
    java.lang.reflect.Method method = connectivityManager.getClass().getMethod("requestRouteToHost", int.class, int.class);
    resultBool = (boolean) method.invoke(connectivityManager, ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress);
} catch (Exception e) { 
    //Do something 
}

以上适用于 targetSDK 27

The above works on targetSDK 27

这篇关于Android O 预览版:ConnectivityManager 删除的方法不向后兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆