我在应用程序中写了一些代码,以检查手机是否有wifi连接,我可以做到这一点很好但它的工作原理但我真正想要检查的是手机没有连接到wifi,但它没有似乎工作。我想我必须在某处弄错语法。请参阅以下代码:
ConnectivityManager connManager = (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
myWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!myWifi.isConnected()){
//do something
}
答案 0 :(得分:0)
试试这个
/ *网络检查* /
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 ..
}
答案 1 :(得分:0)
您应该使用BroadcastReciever获取活动中wifi连接的更新。以下是示例代码
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"));
}
}
还在 AndroidManifest.xml 文件
中写入<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
权限
答案 2 :(得分:0)
使用您的清单中的ACCESS_NETWORK_STATE
权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
使用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;
}
您接下来要做的就是在您的条件下调用该方法。实施例
if(notConnectedToWifi){
// Oops! You're not connected to wifi!
}
请注意,我检查activeNetwork
是否通过activeNetwork.isConnected()
连接,因此如果设备连接到移动网络,则notConnectedToWifi()
方法将返回false。如果它没有连接到任何网络,它也将返回false。
答案 3 :(得分:0)
在ConnectivityManager的帮助下,您将根据跟踪获取 WIFI 是否已连接的网络信息。
检查以下代码(传递活动上下文):
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;
}