当指定仅使用wifi时ConnectivityManager网络为空

2020年8月8日 13点热度 0条评论

我正在尝试通过ConnectivityManager存储wifi网络(并最终存储为蜂窝网络),以供以后在我的程序中使用,以专门通过本地Wifi或蜂窝网络同时发送数据。我已经按照这篇文章(Make Android simultaneously use WiFi to talk to a device and mobile data to talk to a server?How to stay connected through mobile network after WIFI is connected on Android?)尝试使其运行。但是mWifiNetwork为空。使用调试模式,变量mWifiNetwork显示为"108",然后在完成“ onAvailable”方法后,变量将返回空值。不知道为什么变量不能正确存储网络。

我创建了一个在onResume中调用的forceWifiNetwork类

forceWifiNetwork.java

public class forceWifiNetwork {

    //setting up a context variable and method to use "getSystemService"
    // this way we do not need to extend MainActivity class to this class
    // But when we initialize this class in main activity we will have to pass in "this" to the class as a parameter

    Context mContext;
    private ConnectivityManager.NetworkCallback mWifiNetworkCallback;
    public Network mWifiNetwork;
    final ConnectivityManager manager;



    public forceWifiNetwork(Context mContext){
        this.mContext = mContext;
        manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    }




    public void cellConnect(){

        if (mWifiNetworkCallback == null){

            // Init only once
            mWifiNetworkCallback = new ConnectivityManager.NetworkCallback(){
                @Override
                public void onAvailable(final Network network){
                    try{
                        //Save this network for later use
                        mWifiNetwork = network;
                    }catch(Exception e){
                        Log.i("onAvailable ERROR","Error in onAvailable method");
                    }
                }
            };
        }

        NetworkRequest.Builder wifiBuilder;
        wifiBuilder = new NetworkRequest.Builder();
        //set the transport type to do wifi
        wifiBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        //wifiBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
        manager.requestNetwork(wifiBuilder.build(),mWifiNetworkCallback);


    }

}


onResume(在MainActivity.java中)

    @Override
    public void onResume(){
        super.onResume();

        try {
            forceWifiNetwork myWifiNetwork = new forceWifiNetwork(this);
            myWifiNetwork.cellConnect();
            if (myWifiNetwork.mWifiNetwork == null) {
                Log.i("onResume", "mWifiNetwork is null");
            } else {
                Log.i("onResume", "mWifiNetwork is not null");
            }
        }catch(Exception e){
            Log.i("ERROR IN ONRESUME","error");
            e.printStackTrace();
        }


    }

我目前在AndroidManifest.xml中也具有这些权限

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

解决方案如下:

forceWifiNetwork而不是onCreate中移动onResume的开头。我相信onAvailable中的ConnectivityManager.Networkcallback方法可能具有一些底层的Async属性。看起来像这样。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //creating network variables that store wifi and cell networks
        myWifiNetwork = new forceWifiNetwork(this);


        try {
            myWifiNetwork.cellConnect();
            if (myWifiNetwork.mWifiNetwork == null) {
                Log.i("onCreate", "mWifiNetwork is null");
            } else {
                Log.i("onCreate", "mWifiNetwork is not null");
            }
        }catch(Exception e){
            Log.i("Error in onCreate","error creating network");
            e.printStackTrace();
        }


    }