我正试图获得用户连接到网络的那一刻,然后我认为这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)
还有更多...但可悲的是我找不到正确的答案.
我正在尝试做的事情是获取用户连接到网络的确切事件,然后获得一个很好的方法来检测我是否可以进行谷歌ping或检查是否有连接到Internet (仅限WIFI,3G)连接是不允许的),因为我现在使用的代码有时会失败...
我认为这是一个很好的方法,知道是否有一个,Internet Connection
因为我想知道的东西是检测Wifi是否需要浏览器登录.
我们差不多完成了......但是我不明白为什么要进入BroadcastReceiver
4次甚至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)
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)
归档时间: |
|
查看次数: |
25950 次 |
最近记录: |