在 class 中检查 Wifi 是否已连接的方法
Method to check if Wifi is Connected with in class
我想在单击按钮时检查 wifi 是否已连接并显示弹出消息,所以我为此单独写了一个 class:
public class 弹出扩展 Activity {
@Override
protected void onCreate(Bundle savedInstanceSate) {
super.onCreate(savedInstanceSate);
setContentView(R.layout.popup);
DisplayMetrics dm = new DisplayMetrics() ;
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
int height = dm.heightPixels ;
getWindow().setLayout( (int)(width*.6),(int)(height*.4) ) ;
}
那么检查wifi是否连接的方法应该是什么,即使应用程序关闭它也会每5秒检查一次?
您可以调用此方法检查您是否可以上网:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
记得同时请求所需的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
试试这个
添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在你的MainActivity.java
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
Intent networkInfoServiceIntent = new Intent(MainActivity.this,NetworkInfoService.class);
startService(networkInfoServiceIntent);
}
创建服务名称为NetworkInfoService
并在清单中注册
现在将 NetworkInfoService
实施为
/**
* Function for check network connectivity after every 5 seconds
*/
private void checkNetworkConnectivity() {
// handler will run after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()){
checkNetworkConnectivity();
}else {
// display alert (notification)
}
}
},5000
);
}
我想在单击按钮时检查 wifi 是否已连接并显示弹出消息,所以我为此单独写了一个 class:
public class 弹出扩展 Activity {
@Override
protected void onCreate(Bundle savedInstanceSate) {
super.onCreate(savedInstanceSate);
setContentView(R.layout.popup);
DisplayMetrics dm = new DisplayMetrics() ;
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
int height = dm.heightPixels ;
getWindow().setLayout( (int)(width*.6),(int)(height*.4) ) ;
}
那么检查wifi是否连接的方法应该是什么,即使应用程序关闭它也会每5秒检查一次?
您可以调用此方法检查您是否可以上网:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
记得同时请求所需的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
试试这个
添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在你的MainActivity.java
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
Intent networkInfoServiceIntent = new Intent(MainActivity.this,NetworkInfoService.class);
startService(networkInfoServiceIntent);
}
创建服务名称为NetworkInfoService
并在清单中注册
现在将 NetworkInfoService
实施为
/**
* Function for check network connectivity after every 5 seconds
*/
private void checkNetworkConnectivity() {
// handler will run after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()){
checkNetworkConnectivity();
}else {
// display alert (notification)
}
}
},5000
);
}