如何实时查看上网情况?
How to check Internet access at realtime?
我发现了很多如何检查互联网访问的主题。但我不明白如何动态地制作它。例如,我发送了一个请求(它将在另一个线程中工作)并且设备失去了 Internet 连接。我如何取消我对此活动的请求?
这是我之前做的东西;也许它会有所帮助:
public class NetworkStateMonitor extends BroadcastReceiver {
Context mContext;
boolean mIsUp;
public interface Listener {
void onNetworkStateChange(boolean up);
}
public NetworkStateMonitor(Context context) {
mContext = context;
//mListener = (Listener)context;
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, intentFilter);
mIsUp = isUp();
}
/**
* call this when finished with it, and no later than onStop(): callback will crash if app has been destroyed
*/
public void unregister() {
mContext.unregisterReceiver(this);
}
/*
* can be called at any time
*/
public boolean isUp() {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isConnectedOrConnecting();
}
/**
* registerReceiver callback, passed to mListener
*/
@Override public void onReceive(Context context, Intent intent) {
boolean upNow = isUp();
if (upNow == mIsUp) return; // no change
mIsUp = upNow;
((Listener)mContext).onNetworkStateChange(mIsUp);
}
}
您可以使用连接管理器
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
这是我的建议,在您的函数中调用此方法以了解互联网是否可用
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
在您的清单中
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
或者您可以将方法放在父 activity 或片段(有条件)中。
我发现了很多如何检查互联网访问的主题。但我不明白如何动态地制作它。例如,我发送了一个请求(它将在另一个线程中工作)并且设备失去了 Internet 连接。我如何取消我对此活动的请求?
这是我之前做的东西;也许它会有所帮助:
public class NetworkStateMonitor extends BroadcastReceiver {
Context mContext;
boolean mIsUp;
public interface Listener {
void onNetworkStateChange(boolean up);
}
public NetworkStateMonitor(Context context) {
mContext = context;
//mListener = (Listener)context;
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, intentFilter);
mIsUp = isUp();
}
/**
* call this when finished with it, and no later than onStop(): callback will crash if app has been destroyed
*/
public void unregister() {
mContext.unregisterReceiver(this);
}
/*
* can be called at any time
*/
public boolean isUp() {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isConnectedOrConnecting();
}
/**
* registerReceiver callback, passed to mListener
*/
@Override public void onReceive(Context context, Intent intent) {
boolean upNow = isUp();
if (upNow == mIsUp) return; // no change
mIsUp = upNow;
((Listener)mContext).onNetworkStateChange(mIsUp);
}
}
您可以使用连接管理器
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
这是我的建议,在您的函数中调用此方法以了解互联网是否可用
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
在您的清单中
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
或者您可以将方法放在父 activity 或片段(有条件)中。