android connect to internet event

后端 未结 4 729
情书的邮戳
情书的邮戳 2020-12-17 02:34

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 t

相关标签:
4条回答
  • 2020-12-17 02:52
    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

    0 讨论(0)
  • 2020-12-17 02:58
    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.

    0 讨论(0)
  • 2020-12-17 03:01

    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);
    
    0 讨论(0)
  • 2020-12-17 03:08

    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.

    0 讨论(0)
提交回复
热议问题