2

I am Trying to test network availability with callback.

I want to show toast notification when connection is not available. When I have access to the internet, onAvailable() works fine.

onAvailable() does not seem to work. Toast with message "No internet connection" does not show up.

I've read some info in the documentation about unAvailable(). Timeout value was mentioned. I am assuming onUnavailable() gets called after this timeout. However, I do not if it is related to the problem.

ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                Toast.makeText(MainActivity.this, "Request successful", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
                Toast.makeText(MainActivity.this, "No internet connection", Toast.LENGTH_LONG).show();
        }
        };
        final ConnectivityManager connectivityManager
                = (ConnectivityManager) MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);


        connectivityManager.registerNetworkCallback(new NetworkRequest.Builder()
                .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .build(), networkCallback);
Im Shyte
  • 21
  • 4

4 Answers4

1

Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare the broadcast receiver in their manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

Here is the link for solution

Mohan Sai Manthri
  • 2,808
  • 1
  • 11
  • 26
0

You should override onLost(Network network) method instead !

Agnaramon
  • 851
  • 10
  • 15
  • 2
    onLost method is called when device loses network(It had one a while ago). It does not get called when user does not have internet in the first place(This is what I'm trying to check). This does not solve my issue – Im Shyte Sep 10 '19 at 11:10
  • with ```connectivityManager.requestNetwork(...)```you can specify the timeout value. But ```onUnavailable()``` is reached once when the app start without internet connection and you should recall ```requestNetwork(...)``` on it for listening connectivity change again. – Agnaramon Sep 10 '19 at 12:32
0

onNetwork disconnection you can get callback onLost()

  val networkCallback = object : ConnectivityManager.NetworkCallback() {
             override fun onAvailable(network: Network) {
                 Toast.makeText(this@MainActivity, "Request successful", Toast.LENGTH_LONG).show()
             }

             override fun onUnavailable() {
                 super.onUnavailable()
                 Toast.makeText(this@MainActivity, "onUnavailable", Toast.LENGTH_LONG).show()
             }

             override fun onLost(network: Network) {
                 super.onLost(network)
                 Toast.makeText(this@MainActivity, "onLost", Toast.LENGTH_LONG).show()
             }


         }
         val connectivityManager = this@MainActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager


         connectivityManager.registerNetworkCallback(NetworkRequest.Builder()
                 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                 .build(), networkCallback)
Neha Rathore
  • 429
  • 2
  • 9
  • onLost method is called when device loses network(It had one a while ago). It does not get called when user does not have internet in the first place(This is what I'm trying to check). This does not solve my issue. Thanks for commenting – Im Shyte Sep 10 '19 at 11:09
  • 1
    ConnectivityManager ConnectMgr = (ConnectivityManager) context.getSystemService("connectivity"); if (ConnectMgr == null) { return false; } else { NetworkInfo NetInfo = ConnectMgr.getActiveNetworkInfo(); return NetInfo == null ? false : NetInfo.isConnected(); // this will give you if you are connected with network or not -- if not then in that case you can show toast message } – Neha Rathore Sep 10 '19 at 11:18
0

Why dont you use broad cast receiver? please check this if you can use broadcast receiver. https://www.tutorialspoint.com/how-to-check-internet-connection-in-android

Sudeep
  • 129
  • 10