如何在 class 文件中创建检查 NetworkConnectionStatus 并在 mainactivity 中使用
how to create check NetworkConnectionStatus in class file and use in mainactivity
在 .class 文件中添加并在 MainActivity.java 中使用
如何在 class 文件中创建检查 NetworkConnectionStatus 并在 mainactivity
中使用
Private void checkNetworkConnectionStatus() {
boolean wifiConnected;
boolean mobileConnected;
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()){ //connected with either mobile or wifi
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
if (wifiConnected){ //wifi connected
}
else if (mobileConnected){ //mobile data connected
}
}
else { //no internet connection
}
}
在清单文件中,
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在 Activity 中,此方法检查手机是否已连接到互联网,如果已连接 returns 则为真:
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnected();
}
在另一个 class 上调用它时只需提供上下文即可。
首先在您的 MainActivity 中全局声明它。
Context context = this;
这就是你如何称呼你当前 activity 上下文提供的 class。
new CheckNet().checkNetworkConnectionStatus(context);
然后使用需要参数的方法更改您的 class:context
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
class CheckNet {
void checkNetworkConnectionStatus(Context context) {
boolean wifiConnected;
boolean mobileConnected;
ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) { //connected with either mobile or wifi
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
if (wifiConnected) { //wifi connected
} else if (mobileConnected) { //mobile data connected
}
} else { //no internet connection
}
}
}
清单文件
<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" />
试试这个
public class NetworkUtil {
public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("www.google.com");
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
}
在 .class 文件中添加并在 MainActivity.java 中使用 如何在 class 文件中创建检查 NetworkConnectionStatus 并在 mainactivity
中使用
Private void checkNetworkConnectionStatus() {
boolean wifiConnected;
boolean mobileConnected;
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()){ //connected with either mobile or wifi
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
if (wifiConnected){ //wifi connected
}
else if (mobileConnected){ //mobile data connected
}
}
else { //no internet connection
}
}
在清单文件中,
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在 Activity 中,此方法检查手机是否已连接到互联网,如果已连接 returns 则为真:
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnected();
}
在另一个 class 上调用它时只需提供上下文即可。
首先在您的 MainActivity 中全局声明它。
Context context = this;
这就是你如何称呼你当前 activity 上下文提供的 class。
new CheckNet().checkNetworkConnectionStatus(context);
然后使用需要参数的方法更改您的 class:context
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
class CheckNet {
void checkNetworkConnectionStatus(Context context) {
boolean wifiConnected;
boolean mobileConnected;
ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) { //connected with either mobile or wifi
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
if (wifiConnected) { //wifi connected
} else if (mobileConnected) { //mobile data connected
}
} else { //no internet connection
}
}
}
清单文件
<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" />
试试这个
public class NetworkUtil {
public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("www.google.com");
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
}