Broadcastreceiver用于检测网络是否已连接

Ski*_*ᴉʞS 23 android

我正试图获得用户连接到网络的那一刻,然后我认为这BroadcastReceiver是一个很好的方法......我想做的事情是,当用户连接到网络时,知道该网络是否已连接到Internet .

主要的是知道WiFi是否需要Browse Log in示例:Handling Network Sign-On文档

到目前为止我尝试过什么?

我把我BroadcastReceiver变成了这个

if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
    Log.d("Network", "Internet YAY");
} else if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) {
    if (isNetworkOnline()) {
        Log.d(TAG, String.valueOf(tikis));
        NetTask TeInternet = new NetTask();
        TeInternet.execute("https://www.google.com");


    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试连接到WiFi没有Internet Connection输入的是这样的:

D/Network? Internet YAY
D/Network? Internet YAY
D/Network? Internet YAY
D/RequiresLoginBroadcast? 1 //this occurs sometimes
Run Code Online (Sandbox Code Playgroud)

我已将文档更改Inner Class为此Handling Network Sign-On内容

doInBackground() 方法:

protected Boolean doInBackground(String...params) {
boolean internetAccessExists = false;
String urlToBeAccessed = params[0];
final int TIMEOUT_VALUE_IN_MILLISECONDS = 15000;
URL url;
HttpURLConnection urlConnection = null;
try {
    url = new URL(urlToBeAccessed);
    urlConnection = (HttpURLConnection) url.openConnection();
    //set the respective timeouts for the connection
    urlConnection.setConnectTimeout(TIMEOUT_VALUE_IN_MILLISECONDS);
    urlConnection.setReadTimeout(TIMEOUT_VALUE_IN_MILLISECONDS);
    //the redirect check is valid only after the response headers have been received
    //this is triggered by the getInputStream() method
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    if (!url.getHost().equals(urlConnection.getURL().getHost())) {
        internetAccessExists = true;
    }
}
//any sort of exception is considered as a redirect.
//more specific exceptions such as SocketTimeOutException and IOException can be handled as well
catch (Exception e) {
    Log.d(TAG, e.toString());
} finally {
    Log.d(TAG, "Finally");
    urlConnection.disconnect();
}
return internetAccessExists;
Run Code Online (Sandbox Code Playgroud)

到目前为止我一直在寻找什么?

  1. 如何检测Android中何时建立WIFI连接?
  2. Android WIFI如何在特定WIFI连接可用时检测
  3. BroadcastReceiver无法正常工作(检测wifi是否已连接)

还有更多...但可悲的是我找不到正确的答案.

TL; DR

我正在尝试做的事情是获取用户连接到网络的确切事件,然后获得一个很好的方法来检测我是否可以进行谷歌ping或检查是否有连接到Internet (仅限WIFI,3G)连接是不允许的),因为我现在使用的代码有时会失败...

我认为这是一个很好的方法,知道是否有一个,Internet Connection因为我想知道的东西是检测Wifi是否需要浏览器登录.

我们差不多完成了......但是我不明白为什么要进入BroadcastReceiver4次甚至5次....有时候会说Internet connection有没有......

Moo*_*oom 23

这就是我目前使用的,它的工作正常.当互联网连接时,我会收到通知(不只是打开,当有实际连接到互联网时).
它也适用于任何类型的数据连接,但可以很容易地修改为只接受WiFi,3G,4G等.

码:

public class NetworkReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
                Log.d("Network", "Internet YAY");
            } else if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) {
                Log.d("Network", "No internet :(");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

表现:

<receiver
    android:name=".receivers.NetworkReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

  • .EXTRA_NETWORK_INFO id已弃用!! 该死的 (7认同)

leo*_*leo 10

public abstract class NetworkReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (null != intent) {
        State wifiState = null;  
        State mobileState = null;  
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
        if (wifiState != null && mobileState != null  
                && State.CONNECTED != wifiState  
                && State.CONNECTED == mobileState) {  
            // phone network connect success
            gNetwork();
        } else if (wifiState != null && mobileState != null  
                && State.CONNECTED != wifiState  
                && State.CONNECTED != mobileState) {  
            // no network
            noNetwork();
        } else if (wifiState != null && State.CONNECTED == wifiState) {  
            // wift connect success
            WIFINetwork();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

清单集

 <receiver android:name=".receiver.AssistantReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)