问题描述
我想在我的android应用程序运行时在后台连续检查互联网连接.我的意思是从应用程序开始到应用程序结束.我该怎么做?应该怎么办?
I want to check internet connectivity continuously in background while my android application running. I mean from starting of the application to the end of the application. How can i do it? What should be the approach?
推荐答案
我知道我来不及回答这个问题了.但这是一种在活动/片段中连续监视网络连接的方法.
I know I am late to answer this Question.But here is a way to monitor the Network Connection Continuously in an Activity/Fragment.
我们将使用网络回调来监视活动/片段中的连接
We will use Network Callback to monitor our connection in an Activity/Fragment
首先,在类中声明两个变量
first, declare two variable in class
// to check if we are connected to Network
boolean isConnected = true;
// to check if we are monitoring Network
private boolean monitoringConnectivity = false;
接下来,我们将编写网络回调方法
Next, We will write the Network Callback Method
private ConnectivityManager.NetworkCallback connectivityCallback
= new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
isConnected = true;
LogUtility.LOGD(TAG, "INTERNET CONNECTED");
}
@Override
public void onLost(Network network) {
isConnected = false;
LogUtility.LOGD(TAG, "INTERNET LOST");
}
};
现在,既然我们已经编写了Callback,我们现在将编写一个方法来监视我们的网络连接,并在此方法中,我们将注册NetworkCallback.
Now Since we have written our Callback we will Now write a method which will monitor our Network Connection, and in this method, we will register our NetworkCallback.
// Method to check network connectivity in Main Activity
private void checkConnectivity() {
// here we are getting the connectivity service from connectivity manager
final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
// Getting network Info
// give Network Access Permission in Manifest
final NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
// isConnected is a boolean variable
// here we check if network is connected or is getting connected
isConnected = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
if (!isConnected) {
// SHOW ANY ACTION YOU WANT TO SHOW
// WHEN WE ARE NOT CONNECTED TO INTERNET/NETWORK
LogUtility.LOGD(TAG, " NO NETWORK!");
// if Network is not connected we will register a network callback to monitor network
connectivityManager.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build(), connectivityCallback);
monitoringConnectivity = true;
}
}
我们所有的工作都快完成了,我们只需要调用我们的Activity/Fragment的onResume()中的方法checkConnectivity()
All our work is almost finished we just need to call our checkConnectivity(), the method in onResume() of our Activity/Fragment
@Override
protected void onResume() {
super.onResume();
checkConnectivity();
}
*在活动/片段方法onPause()中取消注册网络回调*
@Override
protected void onPause() {
// if network is being moniterd then we will unregister the network callback
if (monitoringConnectivity) {
final ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.unregisterNetworkCallback(connectivityCallback);
monitoringConnectivity = false;
}
super.onPause();
}
就是这样,只需添加此代码即可在其中监视网络的活动/片段的每个类!并且不要忘记注销它!
That's it, just add this code in which every class of your Activity/Fragment were you need to monitor the Network!.And don't forget to unregister it!!!
这篇关于android应用程序运行时如何在后台连续检查互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!