Android检查WIFI状态(断开或用户更改WIFI)如何标记?

我有一种方式来标记,如果一个WIFI连接断开/脱落或如果用户实际上改变了WIFInetworking?

我需要我的应用程序来做:连接到WIFI XYZ,如果XYZ得到断开(标记1)或脱落然后重新连接到XYZ。 但用户更改为另一个WiFi BTOpen(FLAG 2),然后允许连接和停止我的服务。 如果用户再次连接到XYZ,则再次启动循环。

我到目前为止是:

<!-- WIFI Receiver --> <receiver android:name=".ReceiverWifi" > <intent-filter> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> <service android:name=".ServiceWifiMonitor" /> <receiver android:name=".ServiceController" > <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver> 

广播接收器:

  myApplication = (MyApplication) context.getApplicationContext(); conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); networkInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); boolean isConnected = networkInfo != null && networkInfo.isConnected(); int reconnectedCount = myApplication.getReconnectedCount(); if (wifiManager.isWifiEnabled()) { if("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { //Start and Stop Service if(myApplication.isReconnect()) startServiceWifiMonitor(); else stopServiceWifiMonitor(); if (isConnected) { //There is a WIFI Connection myApplication.setConnectedWifi(NetworkUtil.getCurrentSSID(context)); myApplication.setWifiStatus("connected"); if (NetworkUtil.isConnectedToXYZ(context)) { startServiceWifiMonitor(); if(pref.getisFirstTime()) { myApplication.setWifiByChoise("XYZ"); pref.setisFirstTime(false); } else { myApplication.setisReconnect(true); } } else { //Connected to different NetWork if(myApplication.isReconnect() && NetworkUtil.isXYZAvailable(context)) { //ReConnect to XYZ NetworkUtil.connectToXYZ(context); myApplication.setReconnectedCount(reconnectedCount++); } else { resetValues("AAAA"); } } }//end if else { if(NetworkUtil.isXYZAvailable(context) && myApplication.getWifiByChoise().equals("XYZ")) { NetworkUtil.connectToXYZ(context); myApplication.setReconnectedCount(reconnectedCount++); } else { resetValues(""); } } }//end CONNECTIVITY_CHANGE 

服务监视器:

 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand > Received start id " + startId + ": " + intent); objHandler.postDelayed(mTasks, 1000); return START_STICKY; }//end onStartCommand private Runnable mTasks = new Runnable() { public void run() { if(myApplication.getWifiByChoise().equals("XYZ") && NetworkUtil.isXYZAvailable(context)) { try { //Get the numbers of Reconnection int count = myApplication.getReconnectedCount(); if(!NetworkUtil.isWifiConnected(context)) { NetworkUtil.connectToXYZ(context); myApplication.setisReconnect(true); myApplication.setReconnectedCount(count++); } if(!NetworkUtil.isConnectedToXYZ(context)) { NetworkUtil.connectToXYZ(context); myApplication.setisReconnect(true); myApplication.setReconnectedCount(count++); } } catch (Exception e) {e.printStackTrace();} } else { stopSelf(); } int ms_interval = 3000; objHandler.postDelayed(mTasks, ms_interval); } };//end Runnable mTasks 

我的应用程序的问题是:它坠毁的设备,好像它吃了所有的内存内存。 有时与WiFi的XYZ得到断开它不会再次连接,如果用户更改为另一个WiFi,它不会允许连接。

我非常感谢你的帮助。 谢谢。

Solutions Collecting From Web of "Android检查WIFI状态(断开或用户更改WIFI)如何标记?"

通过使用检查networking连接的名称

 public String getWifiName(Context context) { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (manager.isWifiEnabled()) { WifiInfo wifiInfo = manager.getConnectionInfo(); if (wifiInfo != null) { DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) { return wifiInfo.getSSID(); } } } return null; } 

如果这个名称与您的networkingSSID(即XYZ)相匹配,则比恢复该服务,否则,如果不匹配,则停止服务

 if getWifiName(this).compareTo("XYZ") == 0 { //XYZ is your network name on which you want to resume the service //code to resume } else { //code to stop the service } 

这是我在我的应用程序中处理它的方式:

 public class WifiStateWatcher extends BroadcastReceiver { private MainActivity activity; public WifiStateWatcher(MainActivity activity) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); } @Override public void onReceive(Context context, Intent intent) { SupplicantState supState; WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); supState = wifiInfo.getSupplicantState(); if (supState.equals(SupplicantState.COMPLETED)) { //we are connected to Wi-Fi network } else { //we lost Wi-Fi connectivity } } } 

您将需要android.permission.ACCESS_WIFI_STATE权限

你所做的几乎是正确的。 你需要检查networkingssd名称与用户连接wifi名称。如果它匹配,然后做你的一部分。

 WifiManager wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifiManager.isWifiEnabled()) { WifiInfo networkInfo = wifiManager.getConnectionInfo(); if (networkInfo != null) { DetailedState state = WifiInfo.getDetailedStateOf(networkInfo .getSupplicantState()); if (state == DetailedState.CONNECTED ) { return networkInfo.getSSID(); } } } return null; 

现在你有networking的SSID,所以尝试检查您的WiFi名称和SSID,那么你会知道连接状态…..快乐编程

也只是检查了这一点,发现主要区别是:

 /** IP traffic should be available. */ DetailedState.CONNECTED 

和:

 /** * … * This state indicates that the supplicant has completed its * processing for the association phase and that data connection is * fully configured. Note, however, that there may not be any IP * address associated with the connection yet. Typically, a DHCP * request needs to be sent at this point to obtain an address. */ SupplicantState.COMPLETED 

所以要相信,无线上网完全是,我现在添加检查:

 boolean isConnected = activeNetworkInfo.isConnected(); 

DetailedState.CONNECTED 🙂

快乐的编码