Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- boolean haveNetworkConnection(android.content.Context context) {
- boolean haveConnectedWifi = false;
- boolean haveConnectedMobile = false;
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
- assert cm != null;
- NetworkInfo[] netInfo = cm.getAllNetworkInfo();
- for (NetworkInfo ni : netInfo) {
- if (ni.getTypeName().equalsIgnoreCase("WIFI"))
- if (ni.isConnected())
- haveConnectedWifi = true;
- if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
- if (ni.isConnected())
- haveConnectedMobile = true;
- }
- return haveConnectedWifi || haveConnectedMobile;
- }
- ## usage
- if(!haveNetworkConnection(getApplicationContext())) {
- Toast.makeText(getApplicationContext(), "No internet connection", Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(getApplicationContext(), "Internet available", Toast.LENGTH_SHORT).show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement