当我在没有连接 WiFi 的情况下打开我的 android 应用程序时,它显示没有连接 WiFi,正如我在代码中给出的那样
when i open my android app when no WiFi connected it show no WiFi connected as i given in the code
但是当我将我的应用程序与 WiFi 连接时,它仍然显示未连接 WiFi,因此我必须关闭应用程序并在连接 WiFi 的情况下再次打开它。
if (wifimanager.setWifiEnabled(true)) {
punchin.setOnClickListener {
if ((conMgr.activeNetworkInfo != null) && (wifi.isAvailable == true && wifi.isConnected == true)) {
Log.v("MAc", "mac=" + wMAC + "\n" + wbssid + "\n" + wssid)
if ((wbssid == "") && (wssid.equals(""))
&& conMgr.activeNetworkInfo != null && conMgr.activeNetworkInfo.isAvailable
&& conMgr.activeNetworkInfo.isConnected) {
textView.text = "Connected to correct Wifi"
punchout.visibility = View.VISIBLE
} else if (wbssid!="") {
Toast.makeText(applicationContext, "Connect To Correct Wifi", Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(this, "No Wifi Connection", Toast.LENGTH_LONG).show()
}
}
}
如果你想监听连接变化状态,你需要添加一个接收器。检查下面的代码 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkWifiConnectionStatus();
registerBroadCastReceiver();
}
private void registerBroadCastReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(receiver, intentFilter);
}
private void checkWifiConnectionStatus() {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiMgr.isWifiEnabled()) {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (wifiInfo.getNetworkId() == -1) {
Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
return;
}
}
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
checkWifiConnectionStatus();
}
};
在您的 activity 内注册广播接收器,并在该接收器内通过连接管理器监视连接状态,当连接状态更改时调用该方法来执行您的任务。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BroadcastReceiver netReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
try {
performTask(isConnected);
} catch (Exception e) {
e.printStackTrace();
}
} else {
performTask(isConnected);
}
}
};
}
public void performTask(boolean isConnected) {
if (isConnected) {
Log.i("test", "connection successfull");
} else {
Log.i("test", "connection failed");
}
}
并在清单中添加以下权限
<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_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
但是当我将我的应用程序与 WiFi 连接时,它仍然显示未连接 WiFi,因此我必须关闭应用程序并在连接 WiFi 的情况下再次打开它。
if (wifimanager.setWifiEnabled(true)) {
punchin.setOnClickListener {
if ((conMgr.activeNetworkInfo != null) && (wifi.isAvailable == true && wifi.isConnected == true)) {
Log.v("MAc", "mac=" + wMAC + "\n" + wbssid + "\n" + wssid)
if ((wbssid == "") && (wssid.equals(""))
&& conMgr.activeNetworkInfo != null && conMgr.activeNetworkInfo.isAvailable
&& conMgr.activeNetworkInfo.isConnected) {
textView.text = "Connected to correct Wifi"
punchout.visibility = View.VISIBLE
} else if (wbssid!="") {
Toast.makeText(applicationContext, "Connect To Correct Wifi", Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(this, "No Wifi Connection", Toast.LENGTH_LONG).show()
}
}
}
如果你想监听连接变化状态,你需要添加一个接收器。检查下面的代码 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkWifiConnectionStatus();
registerBroadCastReceiver();
}
private void registerBroadCastReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(receiver, intentFilter);
}
private void checkWifiConnectionStatus() {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiMgr.isWifiEnabled()) {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (wifiInfo.getNetworkId() == -1) {
Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
return;
}
}
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
checkWifiConnectionStatus();
}
};
在您的 activity 内注册广播接收器,并在该接收器内通过连接管理器监视连接状态,当连接状态更改时调用该方法来执行您的任务。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BroadcastReceiver netReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
try {
performTask(isConnected);
} catch (Exception e) {
e.printStackTrace();
}
} else {
performTask(isConnected);
}
}
};
}
public void performTask(boolean isConnected) {
if (isConnected) {
Log.i("test", "connection successfull");
} else {
Log.i("test", "connection failed");
}
}
并在清单中添加以下权限
<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_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />