Skip to content Skip to sidebar Skip to footer

Android Connect To Internet Event

Is there an event that tells me that the device has connected to the INTERNET (3G or wifi)? I need to start some requests only after the device connects to the INTERNET. The code n

Solution 1:

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

Here the doc

Ex:

broadcastReceiver = newBroadcastReceiver() {

                @OverridepublicvoidonReceive(Context context, Intent intent) {

                    ConnectivityManagerconnectivity= (ConnectivityManager) context
                            .getSystemService(Context.CONNECTIVITY_SERVICE);


                        NetworkInfo[] info = connectivity.getAllNetworkInfo();
                        //Play with the info about current network state


                    }

                }
            };

            intentFilter = newIntentFilter();
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            registerReceiver(broadcastReceiver, intentFilter);

Solution 2:

Use a Broadcast receiver which will get called whenever the network state changes:

privateNetworkStateReceiver mNetSateReceiver = null;

privateclassNetworkStateReceiverextendsBroadcastReceiver
{
    @OverridepublicvoidonReceive( Context context, Intent intent )
    {
        // Check the network state to determine whether// we're connected or disconnected
    }
}

@OverridepublicvoidonCreate()
{
    registerReceiver( mNetSateReceiver, newIntentFilter(
        ConnectivityManager.CONNECTIVITY_ACTION ) );
}

@OverridepublicvoidonDestroy()
{
    save();
    unregisterReceiver( mNetSateReceiver );
}

onReceive will get called whenever the network state changes, and you can use the techniques detailed in the other answer to determine whether you're actually connected or not.

Solution 3:

Using this function you are able to know device have internet connected of not:

publicstaticbooleanconnectionCheck(final Context context)
{   
    boolean returnTemp=true;
    ConnectivityManagerconManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfoi= conManager.getActiveNetworkInfo();
    if ((i == null)||(!i.isConnected())||(!i.isAvailable()))
    {
        AlertDialog.Builderdialog=newBuilder(context);
        dialog.setTitle("CONNECTION STATUS");
        dialog.setMessage("Failed");
        dialog.setCancelable(false);
        dialog.setPositiveButton("Ok",newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int which) {
                WifiManagerwifiManager= (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
        Toast.makeText(TennisAppActivity.mContext,"Wi-Fi On", Toast.LENGTH_LONG).show();
            }
        });
        dialog.show();
        returnfalse;
    }
    returntrue;`enter code here`
}

Solution 4:

publicstaticbooleancheckInternetConnection(Context context) {

    finalConnectivityManagermConnectivityManager= (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    finalNetworkInfonetInfo= mConnectivityManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        returntrue;
    } elsereturnfalse;
}

Use this function, function will return true if internet is connected else false

Post a Comment for "Android Connect To Internet Event"