0

我正在尝试通过 ConnectivityManager 存储 wifi 网络(最终是蜂窝网络),以便稍后在我的程序中使用,以便同时通过本地 Wifi 或蜂窝网络发送数据。我已经关注了这篇文章(让 Android 同时使用 WiFi 与设备通信和移动数据与服务器通信?以及在 Android 上连接 WIFI 后如何通过移动网络保持连接?)尝试让它运行。但是mWifiNetwork为空。使用调试模式,当 '''onAvailable''' 方法完成时,变量会显示为空值mWifiNetwork"108"不知道为什么变量没有正确存储网络。

我创建了一个在 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" />
4

1 回答 1

0

forceWifiNetwork移动inonCreate而不是的起始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();
        }


    }

于 2019-10-03T18:15:54.220 回答