本文介绍了getAllNetworkInfo()是德$ P $并购pcated但其更换有差异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ConnectivityManager的getAllNetworkInfo()是pcated在23 API和评论它说用getAllNetworks(),而不是去$ P $。
但是我发现,这些不具有相同的行为。

例如,如果手机有一个活跃的蜂窝网络可用,但目前WiFi是打开的,那么getAllNetworkInfo()将返回两个网络(它会显示无线连接作为和蜂窝为断开连接)。

不过getAllNetwork()只返回在这种情况下,WiFi网络。如果无线网络被关闭,然后将返回蜂窝网络。换句话说,它似乎是仅返回当前活动的网络(但是存在对另一种方法是getActiveNetworkInfo())。

除了getAllNetworkInfo()只返回一个网络,getAllNetworks()也只有退休一个网络。

通过棉花糖,我怎么能得到相同的行为getAllNetworkInfo()即获取可用,他们是否断开或连接的所有网络的列表?

最后,我想知道,如果蜂窝数据连接。目前,随着新ConnectivityManager API,我看不到这样做的任何方式。

如果在code以下是符合SDK 22和运行对并购则列出了两个网络,如果getAllNetworkInfo()被交换为getAllNetworks()(和网络12相应的变化; - >的NetworkInfo)和SDK23编译和相同的设备上运行,只有一个网络被列出。

 公共静态同步无效checkNetworkConnectivity()
{
        上下文语境;
        上下文= CityIdApplication.getHandsetState()的getContext()。
        ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);         的NetworkInfo [] =网络cm.getAllNetworkInfo();
        //网[] =网络cm.getAllNetworks();
        如果(网络!= NULL){
            对于(网络网络:网络){
                的NetworkInfo信息= cm.getNetworkInfo(网络);
                如果(info.isAvailable()){
                    如果(info.isConnected()){
                        Log.v(TAG,==网络类型:+ info.getTypeName()+[+ info.getSubtypeName()
                                +],状态:+ info.getDetailedState());
                    }其他
                        Log.v(TAG,==网络类型:+ info.getTypeName()+[+ info.getSubtypeName()
                                +],状态:+ info.getDetailedState()+=== isAvailable);
                }其他
                    Log.v(TAG,==网络类型:+ info.getTypeName()+[+ info.getSubtypeName()
                            +],状态:+ info.getDetailedState()+===不可用);
            }
    }


解决方案

你为什么不使用 registerNetworkCallback

  NetworkRequest要求=新NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    。建立();
最后ConnectivityManager经理= getSystemService(ConnectivityManager.class);
manager.registerNetworkCallback(要求,新ConnectivityManager.NetworkCallback(){
    @覆盖
    公共无效onAvailable(网络网络){
        的NetworkInfo信息= manager.getNetworkInfo(网络);
        Log.v(TAG,==网络类型:+ info.getTypeName()+[+ info.getSubtypeName()+],状态:+ info.getDetailedState());
    });

ConnectivityManager's getAllNetworkInfo() is deprecated on API 23 and the comments for it say to use getAllNetworks() instead.However I am finding these do not have the same behavior.

For example, if the phone has an active cellular network available but wifi is currently turned on, then getAllNetworkInfo() will return both networks (it'll show wifi as connected and cellular as disconnected).

However getAllNetwork() only returns the wifi network in this situation. If wifi is turned off then it will return the cellular network. In other words it appears to be returning the currently active network only (however there is another method for that which is getActiveNetworkInfo()).

In addition to getAllNetworkInfo() only returning one network, getAllNetworks() is also only retiring one network.

With Marshmallow, how can I get the same behavior as getAllNetworkInfo() i.e. get a list of all networks that are available, whether they are disconnected or connected?

Ultimately I want to know if a cellular data connection is available. At the moment, with the new ConnectivityManager API, I can't see any way of doing this.

If the code below is complied with SDK 22 and run on M then it lists two networks, if getAllNetworkInfo() is swapped for getAllNetworks() (and corresponding changes for Network<->NetworkInfo) and compiled with SDK23 and run on the same device, only one network is listed.

 public static synchronized void checkNetworkConnectivity ()
{
        Context context;
        context = CityIdApplication.getHandsetState().getContext();
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

         NetworkInfo [] networks = cm.getAllNetworkInfo();
        //Network[] networks = cm.getAllNetworks();
        if (networks != null) {
            for (Network network : networks) {
                NetworkInfo info = cm.getNetworkInfo(network);
                if (info.isAvailable()) {
                    if (info.isConnected()) {
                        Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName()
                                + "], state: " + info.getDetailedState());
                    } else
                        Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName()
                                + "], state: " + info.getDetailedState() + "=== isAvailable");
                } else
                    Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName()
                            + "], state: " + info.getDetailedState() + "=== NOT Available");
            }
    }
解决方案

Why don't you use registerNetworkCallback?

NetworkRequest request = new NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    .build();
final ConnectivityManager manager = getSystemService(ConnectivityManager.class);
manager.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback(){
    @Override
    public void onAvailable(Network network) {
        NetworkInfo info = manager.getNetworkInfo(network);
        Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName() + "], state: " + info.getDetailedState());
    });

这篇关于getAllNetworkInfo()是德$ P $并购pcated但其更换有差异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 04:23