用于检查 Internet 连接的布尔方法仅返回 false
Boolean Method for checking Internet Connectivity returning only false
在我的应用程序中,我正在使用 WIFI 或移动数据连接检查手机是否已连接到互联网,但问题是我的代码始终显示无法通过我的设备连接到 WIFI 进行互联网连接。
这是我检查互联网连接的代码。
public class InternetDetector {
private Context mcontext;
public InternetDetector(Context context) {
this.mcontext = context;
}
public boolean checkMobileInternetConn() {
ConnectivityManager connectivity = (ConnectivityManager) mcontext
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
else if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
return false;
}
}
这就是我在其他活动中使用此 class 来检查 connection.But 代码始终转到 else 部分并始终显示 toast 的方式。
if (allValid) {
isConnectionExist = internetDetector.checkMobileInternetConn();
if (isConnectionExist) {
try {
new MyAsyncClass().execute();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
}
我在清单文件下授予的权限是:-
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
替代解决方案:
public boolean hasConnectivity() {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null;
}
我用 hasConnectivity()
替换了 isConnectionExist 变量
if (allValid) {
if (hasConnectivity()) {
try {
new MyAsyncClass().execute();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
}
fun isOnline(context: Context): Boolean {
try {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val nInfo = cm.activeNetworkInfo
return nInfo != null && nInfo.isConnected
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
在我的应用程序中,我正在使用 WIFI 或移动数据连接检查手机是否已连接到互联网,但问题是我的代码始终显示无法通过我的设备连接到 WIFI 进行互联网连接。
这是我检查互联网连接的代码。
public class InternetDetector {
private Context mcontext;
public InternetDetector(Context context) {
this.mcontext = context;
}
public boolean checkMobileInternetConn() {
ConnectivityManager connectivity = (ConnectivityManager) mcontext
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
else if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
return false;
}
}
这就是我在其他活动中使用此 class 来检查 connection.But 代码始终转到 else 部分并始终显示 toast 的方式。
if (allValid) {
isConnectionExist = internetDetector.checkMobileInternetConn();
if (isConnectionExist) {
try {
new MyAsyncClass().execute();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
}
我在清单文件下授予的权限是:-
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
替代解决方案:
public boolean hasConnectivity() {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null;
}
我用 hasConnectivity()
替换了 isConnectionExist 变量if (allValid) {
if (hasConnectivity()) {
try {
new MyAsyncClass().execute();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
}
fun isOnline(context: Context): Boolean {
try {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val nInfo = cm.activeNetworkInfo
return nInfo != null && nInfo.isConnected
} catch (e: Exception) {
e.printStackTrace()
return false
}
}