[英]Android call a check for internet connection automatically
I'd like to know if there's a mechanism so whenever I try to use internet, somehow AUTOMATICALLY behind the scences this method (isNetworkAvailable) will be called. 我想知道是否有一种机制,因此每当我尝试使用Internet时,都会自动以某种方式自动调用此方法(isNetworkAvailable)背后的场景。
The question is NOT about how to check for internet connection as I already know that: 问题不在于如何检查互联网连接,因为我已经知道:
public boolean isNetworkAvailable(){
ConnectivityManager cm =
(ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
return isConnected;
}
To clarify myself, I want to prevent from calling isNetworkAvailable() each time I use internet connection (either by my classes which I call explicitly or by third party classes I use that opens internet connections by themselves), so my code won't be filled everywhere with: 为了澄清自己,我想防止每次使用Internet连接时都调用isNetworkAvailable()(无论是通过我明确调用的类,还是使用自己打开互联网连接的第三方类),因此我的代码不会到处充满:
if(isNetworkAvailable()){
// send message to server or open some other internet connection
}
But this function somehow will be called before each attempt to connect internet. 但是,每次尝试连接Internet之前都会以某种方式调用此函数。
Hope I've cleared myself. 希望我已经清除自己。 Couldn't find a solution on SO.
找不到关于SO的解决方案。
PS My app CAN run on offline mode, so I don't want a simple broadcast reciever that blocks the app whenever there's no internet connection (like some apps do). PS我的应用程序可以在脱机模式下运行,所以我不希望有一个简单的广播接收器在没有互联网连接时就阻止该应用程序(就像某些应用程序一样)。
I have used BroadCastReceiver, which will check my Internet connectivity automatically. 我使用了BroadCastReceiver,它将自动检查我的Internet连接。
NetworkUtil.java NetworkUtil.java
public class NetworkUtil {
public static final int NOT_CONNECTED = 0;
public static final int WIFI = 1;
public static final int MOBILE = 2;
public static int getConnectionStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return WIFI;
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return MOBILE;
}
return NOT_CONNECTED;
}
}
NetworkConnectivityCheck.java NetworkConnectivityCheck.java
public class NetworkConnectivityCheck {
public boolean internetAvailable = false;
private BroadcastReceiver networkChangeReceiver;
public NetworkConnectivityCheck() {
this.networkChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int networkState = NetworkUtil.getConnectionStatus(context);
if (networkState == NetworkUtil.NOT_CONNECTED) {
Log.i("InternetApplication", "Internet is not available.");
internetAvailable = false;
getInternetStatus();
} else if (networkState == NetworkUtil.MOBILE) {
Log.i("InternetApplication", "Internet is available through mobile data.");
internetAvailable = true;
getInternetStatus();
} else if (networkState == NetworkUtil.WIFI) {
Log.i("InternetApplication", "Internet is available through wifi.");
internetAvailable = true;
getInternetStatus();
}
}
};
}
public boolean getInternetStatus(){
Log.d("InternetApplication", "Value of Internet is " + String.valueOf(internetAvailable));
return this.internetAvailable;
}
public void register(Context context){
context.registerReceiver(networkChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
public void unregister(Context context){
context.unregisterReceiver(networkChangeReceiver);
}
}
After these two files, just initialise the class before onCreate Method , it will look like this. 在这两个文件之后,只需在onCreate Method之前初始化类 ,它将看起来像这样。
MainActivity.java MainActivity.java
//Internet Connectivity Check
private NetworkConnectivityCheck status = new NetworkConnectivityCheck();
public void networkStatusCheck(){}
@Override
protected void onCreate(Bundle savedInstanceState) {
networkStatusCheck();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
}
below code helps for you, 下面的代码为您提供帮助,
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Do something
Log.d("Netowk Available ", "Flag No 1");
}
}
}
in manifest, 明显地
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.