查找是否有活动的互联网连接 java android
find if there is an active internet connection java android
我正在尝试了解我的应用程序中是否存在活动的互联网连接。
我需要知道我是否可以毫无错误地访问网络,有时我会收到 "true" 响应,即使我没有互联网连接,那是因为我仍然连接到 WiFi 但没有互联网连接。
同时,我的上网查询功能是:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
此功能只检查我是否连接到某个 WiFi 或移动网络,而不会实际检查我是否可以访问网络。
所以我添加了下一个功能:
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
我不知道为什么,但这个功能总是认为我的互联网连接是好的,即使我的路由器没有连接(我在网栏上有一个黄色的小三角形,显示已连接但没有互联网连接)。
如果连接是 true/false,我每次调用此函数时都尝试打印,并且在连接时我得到 true,拔掉路由器后 5-10 秒我得到 false ,然后我又变得真实了......(没有连接到其他 WiFi 网络)
我已经尝试在 ADB 模拟器和实际设备上进行测试,它们都崩溃了,因为我试图访问网络,即使我是 "offline"。
求助!
非常感谢。
而不是 isConnected() 方法
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
试试这个
return activeNetwork !=null && activeNetwork.isConnectedOrConnecting();
这是实用程序class我通常使用:
public class NetworkHelper {
//method that checks for network status
public static boolean hasNetworkAccess(Context context){
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork !=null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
确保你有权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
在你的 activity 中创建一个
private boolean networkOk;
然后用它看看你的连接是否正常
前
networkOk = NetworkHelper.hasNetworkAccess(this);
if (networkOk) {
Intent intent = new Intent(MainActivity.this, Myservice.class);
intent.setData(Uri.parse(jsonurl));
startService(intent);
}
这对我有用:
public boolean hasNetworkAccess() {
if (hasActiveInternetConnection()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.screens.company").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
System.out.println("NETWORK CHECK");
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e("NETWORK", "Error checking internet connection.");
}
} else {
Log.d("NETWORK", "No network available!");
}
return false;
}
public boolean hasActiveInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
System.out.println("Debug: [Network] Status: " + (activeNetwork != null && activeNetwork.isConnectedOrConnecting()));
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
System.out.println("Debug: [Network] Status: NOT CONNECTED.");
e.printStackTrace();
return false;
}
}
这将实际检查互联网
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetStatus {
private static NetStatus instance = new NetStatus();
static Context context;
ConnectivityManager connectivityManager;
boolean connected = false;
public static NetStatus getInstance(Context ctx) {
context = ctx.getApplicationContext();
return instance;
}
public boolean isOnline() {
boolean online = false;
try {
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
if (connected) {
if (isInternetWorking()) online = true;
}
} catch (Exception e) {
System.out.println("CheckConnectivity Exception: " + e.getMessage());
Log.v("connectivity", e.toString());
}
return online;
}
public boolean isInternetWorking() {
boolean success = false;
try {
URL url = new URL("http://www.google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Android");
connection.setRequestProperty("Connection", "close");
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.connect();
if (connection.getResponseCode() == 200) success = true;
connection.disconnect();
} catch (IOException e) {
//e.printStackTrace();
}
return success;
}
}
我正在尝试了解我的应用程序中是否存在活动的互联网连接。 我需要知道我是否可以毫无错误地访问网络,有时我会收到 "true" 响应,即使我没有互联网连接,那是因为我仍然连接到 WiFi 但没有互联网连接。
同时,我的上网查询功能是:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
此功能只检查我是否连接到某个 WiFi 或移动网络,而不会实际检查我是否可以访问网络。
所以我添加了下一个功能:
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
我不知道为什么,但这个功能总是认为我的互联网连接是好的,即使我的路由器没有连接(我在网栏上有一个黄色的小三角形,显示已连接但没有互联网连接)。
如果连接是 true/false,我每次调用此函数时都尝试打印,并且在连接时我得到 true,拔掉路由器后 5-10 秒我得到 false ,然后我又变得真实了......(没有连接到其他 WiFi 网络)
我已经尝试在 ADB 模拟器和实际设备上进行测试,它们都崩溃了,因为我试图访问网络,即使我是 "offline"。
求助! 非常感谢。
而不是 isConnected() 方法
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
试试这个
return activeNetwork !=null && activeNetwork.isConnectedOrConnecting();
这是实用程序class我通常使用:
public class NetworkHelper {
//method that checks for network status
public static boolean hasNetworkAccess(Context context){
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork !=null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
确保你有权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
在你的 activity 中创建一个
private boolean networkOk;
然后用它看看你的连接是否正常 前
networkOk = NetworkHelper.hasNetworkAccess(this);
if (networkOk) {
Intent intent = new Intent(MainActivity.this, Myservice.class);
intent.setData(Uri.parse(jsonurl));
startService(intent);
}
这对我有用:
public boolean hasNetworkAccess() {
if (hasActiveInternetConnection()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.screens.company").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
System.out.println("NETWORK CHECK");
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e("NETWORK", "Error checking internet connection.");
}
} else {
Log.d("NETWORK", "No network available!");
}
return false;
}
public boolean hasActiveInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
System.out.println("Debug: [Network] Status: " + (activeNetwork != null && activeNetwork.isConnectedOrConnecting()));
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
System.out.println("Debug: [Network] Status: NOT CONNECTED.");
e.printStackTrace();
return false;
}
}
这将实际检查互联网
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetStatus {
private static NetStatus instance = new NetStatus();
static Context context;
ConnectivityManager connectivityManager;
boolean connected = false;
public static NetStatus getInstance(Context ctx) {
context = ctx.getApplicationContext();
return instance;
}
public boolean isOnline() {
boolean online = false;
try {
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
if (connected) {
if (isInternetWorking()) online = true;
}
} catch (Exception e) {
System.out.println("CheckConnectivity Exception: " + e.getMessage());
Log.v("connectivity", e.toString());
}
return online;
}
public boolean isInternetWorking() {
boolean success = false;
try {
URL url = new URL("http://www.google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Android");
connection.setRequestProperty("Connection", "close");
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.connect();
if (connection.getResponseCode() == 200) success = true;
connection.disconnect();
} catch (IOException e) {
//e.printStackTrace();
}
return success;
}
}