Android internet event connection

Is there an event that tells me that the device is connected to the Internet (3G or Wi-Fi)? I need to run some request only after the device connects to the Internet. The code must support Android 2.1. Thanks

+6
source share
4 answers

You can use the Broadcast receiver and wait for the ConnectivityManager.CONNECTIVITY_ACTION

Here doc

Example:

 broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] info = connectivity.getAllNetworkInfo(); //Play with the info about current network state } } }; intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(broadcastReceiver, intentFilter); 
+6
source

Use the broadcast receiver that will be called when the network state changes:

 private NetworkStateReceiver mNetSateReceiver = null; private class NetworkStateReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { // Check the network state to determine whether // we're connected or disconnected } } @Override public void onCreate() { registerReceiver( mNetSateReceiver, new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION ) ); } @Override public void onDestroy() { save(); unregisterReceiver( mNetSateReceiver ); } 

onReceive will be called every time the network state changes, and you can use the methods detailed in another answer to determine if you are really connected or not.

+2
source
 public static boolean connectionCheck(final Context context) { boolean returnTemp=true; ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo i = conManager.getActiveNetworkInfo(); if ((i == null)||(!i.isConnected())||(!i.isAvailable())) { AlertDialog.Builder dialog = new Builder(context); dialog.setTitle("CONNECTION STATUS"); dialog.setMessage("Failed"); dialog.setCancelable(false); dialog.setPositiveButton("Ok",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true); Toast.makeText(TennisAppActivity.mContext,"Wi-Fi On", Toast.LENGTH_LONG).show(); } }); dialog.show(); return false; } return true;`enter code here` } 

Using this function, you can know that the device is connected to the Internet. Hope this helps you.

+1
source
 public static boolean checkInternetConnection(Context context) { final ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } else return false; } 

Use this function, the function will return true if the Internet is connected otherwise false

0
source

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


All Articles