`
dongya1987
  • 浏览: 47227 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Android中写与Wifi相关程序的注意事项——读LocationManagerService有感

阅读更多

1. 检测是否有wifi可用:

mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (mWifiManager != null) {
                    List<ScanResult> wifiScanResults = mWifiManager.getScanResults();
                    if (wifiScanResults != null && wifiScanResults.size() != 0) {
                        
                    }
                }

 2. WifiLock的应用:

    这儿有一篇详细的文章介绍http://malaqu.com/?p=1203

WifiLock Allows an application to keep the Wi-Fi radio awake. Normally the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released. Multiple applications may hold WifiLocks, and the radio will only be allowed to turn off when no WifiLocks are held in any application.
Before using a WifiLock, consider carefully if your application requires Wi-Fi access, or could function over a mobile network, if available. A program that needs to download large files should hold a WifiLock to ensure that the download will complete, but a program whose network usage is occasional or low-bandwidth should not hold a WifiLock to avoid adversely affecting battery life.
Note that WifiLocks cannot override the user-level “Wi-Fi Enabled” setting, nor Airplane Mode. They simply keep the radio from turning off when Wi-Fi is already on but the device is idle.

 

3. 在程序中注册WifiManager.SCAN_RESULTS_AVAILABLE_ACTION和WifiManager.WIFI_STATE_CHANGED_ACTION这两个intent,以关注Wifi的状态,要是程序有需求在wifi可用时做一些操作,这个很有用。

分享到:
评论
2 楼 sunnygql 2013-05-06  

还有那个WIFI_STATE_CHANGED_ACTION这个监听,关闭wifi的时候执行两边,开启wifi的时候执行两边。不知道为什么,就像横竖屏一样。
1 楼 sunnygql 2013-05-06  
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION这个监听,如果有可用的wifi,并且wifi没有连任何wifi的话,会一直执行,一旦连接成功测不在执行;而且执行频率较高;

相关推荐

Global site tag (gtag.js) - Google Analytics