0

I have connected my android phone with LAN connection. I would like to display a toast or an alert when I disable LAN connection. I am trying the following code but its of no use. What am I missing ?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    isNetworkAvailable(MainActivity.this);
}

// to check connection state

public static boolean isNetworkAvailable(Context context){
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Network network = connectivityManager.getActiveNetwork();
    NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
    if(networkCapabilities == null){
        Toast.makeText(context, "No Internet connection!", Toast.LENGTH_LONG).show();
    }
    return true;
}
}

I couldn't Use NetworkInfo since its been deprecated. Thanks in advance

UPDATE:

I solved my problem using the example from the link below. Very well explained. Have a look :)

Detect internet Connection

cantona_7
  • 1,127
  • 4
  • 21
  • 40

3 Answers3

2

First of all the code you showed will be triggered only when activity is created and only once. What you need is Monitor for changes in connectivity

As mentioned:

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

Meaning that you will have to create a custom broadcast receiver (I use kotlin for android development, you should be able to translate that to java):

class NetworkStatusReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        // check intent action for the connection status
    }
}

To use the receiver, you need to register it in your activity (a better choice would be to use a service if you need it across multiple activities):

val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
filter.addAction("mypackage.CONNECTIVITY_CHANGE")
val permissionReceiver = NetworkStatusReceiver()
registerReceiver(permissionReceiver, filter)

To avoid memory leaks or multiple bindings it is recommended to unbind the receiver once the lifecycle of you component ends, you can do that by:

unregisterReceiver(permissionReceiver)

Also don't forget to add to your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Daniel
  • 2,320
  • 1
  • 14
  • 27
  • 1
    this is really helpful. Clear insight. Thanks man ! I will look for a solution based upon your explanation – cantona_7 Aug 28 '19 at 14:18
  • 1
    the solution I provided works since API level 1. If your minimum api is 21+, you can take a look at this https://developer.android.com/reference/android/net/ConnectivityManager.html#requestNetwork(android.net.NetworkRequest,%20android.app.PendingIntent) – Daniel Aug 28 '19 at 16:34
1

Try this

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(isNetworkConnected){

}else{
    Toast.makeText(context, "No Internet 
connection!",Toast.LENGTH_LONG).show();

}
}

public boolean isNetworkConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) 
context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}
}
1

First added network permissions in you manifest.xml file: ex:

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

Then create and register a broadcast receiver class which will be notified whenever there is a change in network/internet connection.

Here is a good example link: https://www.androidhive.info/2012/07/android-detect-internet-connection-status/

Manoj Kumar
  • 332
  • 1
  • 7