简体   繁体   English

android连接到互联网事件

[英]android connect to internet event

Is there a event which tells me that the device has connected to INTERNET (3G or wifi)? 是否有事件告诉我设备已连接到INTERNET(3G或wifi)? I need to start some request only after device connects to INTERNET. 我需要在设备连接到INTERNET后才启动一些请求。 The code needs to support Android 2.1. 该代码需要支持Android 2.1。 Thanks 谢谢

You can use a Broadcast receiver and wait for the action ConnectivityManager.CONNECTIVITY_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);

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. 每当网络状态发生变化时,onReceive都会被调用,您可以使用其他答案中详述的技术来确定您是否实际连接。

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. 我希望这对你充满帮助。

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 使用此功能,如果互联网连接,函数将返回true,否则返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM