ConnectivityManager:
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE)
.
NetworkInfo:
Describes the status of a network interface.
Use ConnectivityManager.getActiveNetworkInfo()
to get an instance that represents the current network connection.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the state of network connectivity
ConnectivityManager cm = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
//an instance that represents the current network connection
NetworkInfo info = cm.getActiveNetworkInfo();
if(info!=null && info.isConnected()){
Toast.makeText(this, "Internet is connected!", 0).show();
}else {
Toast.makeText(this, "Internet is not connected!", 0).show();
Intent intent = new Intent();
//skip to the set of network
intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
startActivity(intent);
}
}
}