android connect to internet event

2019-01-24 12:45发布

问题:

Is there a event which tells me that the device has connected to INTERNET (3G or wifi)? I need to start some request only after device connects to INTERNET. The code needs to support Android 2.1. Thanks

回答1:

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

Here the doc

Ex:

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);


回答2:

Use a Broadcast receiver which will get called whenever 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 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.



回答3:

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 are able to know device have internet connected of not. I hope this is help full to you.



回答4:

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, function will return true if internet is connected else false