How to check Internet access on Android?

What I really want, when the user has no connection, I want to show some kind of dialogue that he has no connection.

I tried putting this in my MainActivity, but still didn't work.

public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo().isConnectedOrConnecting(); } 

I also used this one, but it also did not work:

 public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE ); if ( activeNetInfo != null ) { Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } if( mobNetInfo != null ) { Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } } } 

I added this to my manifest:

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

Can someone tell me what I'm doing wrong, I would really appreciate it. Thanks in advance!

+4
source share
5 answers

Maybe this link may help you a bit: Broadcastreceiver for information on ServiceState

You could skip the CommunicationManager code you specified in your activity or, if you want, access more actions, you can also declare the class static in the Application Singletin instance and call it there, as described here

You also need to register BroadcastReceiver in the manifest of your application:

 <receiver android:enabled="true" android:name="com.project.mobile.controller.comm.ConnectivityReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver> 

Enjoy

+2
source
  public static boolean haveInternet(Context ctx) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } return true; } 

If the boolean method returns true, then there is an Internet connection, if false No Connection ..

 if(haveInternet==true){ //Some code }else{ Toast.makeText(ActivityName.this,"No Internet Connection", Toast.LENGTH_SHORT).show(); } 

Also see this for further assistance .. Internet connection ...

+2
source

Just use this method

 private boolean isNetworkConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = cm.getActiveNetworkInfo(); if (network != null) { return network.isAvailable(); } return false; } 
+1
source

u can test your connection using the ConnectivityManager class. I gave one function that can be used in code.

  public static boolean CheckInternet(Context context) { ConnectivityManager connec =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); android.net.NetworkInfo wifi = > connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI); android.net.NetworkInfo mobile =connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

Here, if Wi-Fi and mobile network status checking is available or not. If either of them is accessible or connected, it will return true, otherwise false;

  if (wifi.isConnected()) { return true; } else if (!mobile.isConnected()) { return false; } else if (mobile.isConnected()) { return true; } return false; } 

after using this function u need 2 just check a condition like

  if (CheckInternet(getBaseContext())) { //connection successfull } else { Toast.makeText(getBaseContext(),"Please Check InternetConnectionToast.LENGTH_LONG).show(); } 

to use

0
source

The above methods work when you are connected to a Wi-Fi source or through cell phone data packets. But in the case of Wi-Fi connection, there are cases when you are asked to enter the system, as in a cafe. Thus, in this case, your application will fail because you are connected to a Wi-Fi source, but not with the Internet.

This method works just fine.

 public static boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null && activeNetwork.isConnected()) { try { URL url = new URL("http://www.google.com/"); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); urlc.setRequestProperty("User-Agent", "test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1000); // mTimeout is in seconds urlc.connect(); if (urlc.getResponseCode() == 200) { return true; } else { return false; } } catch (IOException e) { Log.i("warning", "Error checking internet connection", e); return false; } } return false; } 

Please use this in a separate thread from the main thread, as it makes a network call and will throw a NetwrokOnMainThreadException if not followed.

0
source

Source: https://habr.com/ru/post/1345542/


All Articles