Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – Android检查WIFI状态(断开或用户更改WIFI)如何标记?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果用户实际上更改了WIFI网络,我有没有办法标记WIFI连接是否断开/掉线?

我需要我的应用程序:
连接到WIFI XYZ,如果XYZ断开(FLAG 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>

广播接收器:

@H_47_9@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,它将不允许连接.

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

解决方法

通过使用检查网络连接名称
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;
}

如果此名称与您的网络SSID(即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
}

大佬总结

以上是大佬教程为你收集整理的java – Android检查WIFI状态(断开或用户更改WIFI)如何标记?全部内容,希望文章能够帮你解决java – Android检查WIFI状态(断开或用户更改WIFI)如何标记?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。