Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

android - ConnectivityManager.NetworkCallback() -> onAvailable(Network network) method is not triggered when device connects to a internal wifi network

I am trying to send telementary data to App Center on out internal wifi network but it is not sending on this network but it does on any external network. When debugging found that Method onAvailable() is not called when device is connected to internal wifi but it does get called when connected to any external wifi.

Below code is from App Center SDK :
appcenterutilsNetworkStateHelper.javaNetworkStateHelper.java. Class NetworkStateHelper -> Method reopen() --> public void onAvailable(Network network) method

Sample Code:

private ConnectivityManager.NetworkCallback mNetworkCallback = new ConnectivityManager.NetworkCallback() 
{

    @Override
    public void onAvailable(Network network) {
        onNetworkAvailable(network);
    }

    @Override
    public void onLost(Network network) {
        onNetworkLost(network);
    }
};

It should call the onAvailable method when connected to an internal wifi network.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to restrict the network type to Wifi.

1. Build the NetworkRequest:

val networkRequest = getNetworkRequest()

private fun getNetworkRequest(): NetworkRequest {
    return NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)    //here!!
        .build()
}

2. Build the NetworkCallback:

val networkCallback = getNetworkCallBack()

private fun getNetworkCallBack(): ConnectivityManager.NetworkCallback {
    return object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {    //when Wifi is on
            super.onAvailable(network)

            Toast.makeText(requireContext(), "Wifi is on!", Toast.LENGTH_SHORT).show()
        }

        override fun onLost(network: Network) {    //when Wifi 【turns off】
            super.onLost(network)

            Toast.makeText(requireContext(), "Wifi turns off!", Toast.LENGTH_SHORT).show()
        }
    }
}

3. Register and Unregister:

fun getConnectivityManager() = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

override fun onResume() {    //start monitoring when in the foreground
    super.onResume()

    getConnectivityManager().registerNetworkCallback(networkRequest, networkCallback)
}

override fun onPause() {    //stop monitoring when not fully visible
    super.onPause()

    getConnectivityManager().unregisterNetworkCallback(networkCallback)
}

4. Don't forget the permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Demo: https://youtu.be/RPRcGbuB6eI


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...