[英]How to check that wifi is NOT connected on android
Ive written some code in an app to check if the phone has a wifi connection, I can do this fine and it works but what I actually want to check for is when the phone is not connected to wifi but it doesn't seem to be working. 我已经在应用程序中编写了一些代码来检查手机是否具有wifi连接,我可以做到这一点并且可以正常工作,但我真正想要检查的是手机未连接至wifi却似乎没有连接时工作。 I think I must have the syntax wrong somewhere.
我认为我在某处一定有语法错误。 please see code below:
请参见下面的代码:
ConnectivityManager connManager = (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
myWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!myWifi.isConnected()){
//do something
}
try this 尝试这个
/* Network check */ / *网络检查* /
public static boolean haveNetworkConnection(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
if (haveNetworkConnection(youractivityname.this)) {
//do your work here ..
}
You should use BroadcastReciever for getting updates of wifi connectivity in your activity. 您应该使用BroadcastReciever来获取活动中wifi连接的更新。 Below is the sample code
下面是示例代码
public class MainActivity extends Activity {
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectionManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
Toast.makeText(MainActivity.this, "Is Wifi connected : "+wifi.isConnected(), Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initViews();
MainActivity.this.registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
}
Also write <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
permission in AndroidManifest.xml file 还要在AndroidManifest.xml文件中写入
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
权限
With the ACCESS_NETWORK_STATE
permission in your manifest 在清单中具有
ACCESS_NETWORK_STATE
权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Using the getActiveNetworkInfo()
method, use this method: 使用
getActiveNetworkInfo()
方法,请使用以下方法:
private boolean notConnectedToWifi(){
final ConnectivityManager conMgr = (ConnectivityManager).getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() != ConnectivityManager.TYPE_WIFI)
return true;
else
return false;
}
All you have to do next is to call the method in your condition. 接下来要做的就是在您的条件下调用该方法。 Example
例
if(notConnectedToWifi){
// Oops! You're not connected to wifi!
}
Note that I check if activeNetwork
is connected via activeNetwork.isConnected()
so if the device is connected to a mobile network, the method notConnectedToWifi()
will return false. 请注意,我会检查
activeNetwork
是否通过activeNetwork.isConnected()
连接,因此如果设备已连接到移动网络,则方法notConnectedToWifi()
将返回false。 It will also return false if it isn't connected to any network. 如果未连接到任何网络,它将也返回false。
With the help of ConnectivityManager , you will get the network info based on trace whether WIFI is connected or not. 借助ConnectivityManager ,您将基于跟踪获取是否连接WIFI的网络信息。
Check below code (pass Activity context): 检查以下代码(通过Activity上下文):
public boolean IsInternetConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo nInfo = connectivity.getActiveNetworkInfo();
if (nInfo != null) {
//do your thing
if (nInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
return true;
}
}
return false;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.